Render sets and set groups tree

This commit is contained in:
Florian Schroedl 2024-11-19 16:43:37 +01:00
parent 1d2c7dd20e
commit a19d85fb10
10 changed files with 373 additions and 237 deletions

View file

@ -410,6 +410,11 @@
[:type [:= :add-token-set]]
[:token-set ::ctot/token-set]]]
[:add-token-sets
[:map {:title "AddTokenSetsChange"}
[:type [:= :add-token-sets]]
[:token-sets [:sequential ::ctot/token-set]]]]
[:mod-token-set
[:map {:title "ModTokenSetChange"}
[:type [:= :mod-token-set]]
@ -427,6 +432,11 @@
[:type [:= :del-token-set]]
[:name :string]]]
[:del-token-set-path
[:map {:title "DelTokenSetPathChange"}
[:type [:= :del-token-set-path]]
[:path :string]]]
[:set-tokens-lib
[:map {:title "SetTokensLib"}
[:type [:= :set-tokens-lib]]
@ -1046,6 +1056,12 @@
(ctob/ensure-tokens-lib)
(ctob/add-set (ctob/make-token-set token-set)))))
(defmethod process-change :add-token-sets
[data {:keys [token-sets]}]
(update data :tokens-lib #(-> %
(ctob/ensure-tokens-lib)
(ctob/add-sets (map ctob/make-token-set token-sets)))))
(defmethod process-change :mod-token-set
[data {:keys [name token-set]}]
(update data :tokens-lib (fn [lib]
@ -1066,6 +1082,12 @@
(ctob/ensure-tokens-lib)
(ctob/delete-set name))))
(defmethod process-change :del-token-set-path
[data {:keys [path]}]
(update data :tokens-lib #(-> %
(ctob/ensure-tokens-lib)
(ctob/delete-set-path path))))
;; === Operations
(def ^:private decode-shape

View file

@ -818,15 +818,15 @@
(update :undo-changes conj {:type :mod-token-set :name (:name token-set) :token-set (or prev-token-set token-set)})
(apply-changes-local)))
(defn delete-token-set
[changes token-set-name]
(defn delete-token-set-path
[changes token-set-path]
(assert-library! changes)
(let [library-data (::library-data (meta changes))
prev-token-theme (some-> (get library-data :tokens-lib)
(ctob/get-set token-set-name))]
prev-token-sets (some-> (get library-data :tokens-lib)
(ctob/get-path-sets token-set-path))]
(-> changes
(update :redo-changes conj {:type :del-token-set :name token-set-name})
(update :undo-changes conj {:type :add-token-set :token-set prev-token-theme})
(update :redo-changes conj {:type :del-token-set-path :path token-set-path})
(update :undo-changes conj {:type :add-token-sets :token-sets prev-token-sets})
(apply-changes-local))))
(defn move-token-set-before

View file

@ -183,6 +183,14 @@
(def set-separator "/")
(defn join-set-path [set-path]
(join-path set-path set-separator))
(defn split-set-prefix [set-path]
(some->> set-path
(re-matches #"^([SG]-)(.*)")
(rest)))
(defn add-set-prefix [set-name]
(str set-prefix set-name))
@ -199,14 +207,29 @@
set-name (add-set-prefix (last paths))]
(conj set-path set-name)))
(defn split-token-set-path [token-set-path]
(split-path token-set-path set-separator))
(defn split-token-set-name [token-set-name]
(-> (split-path token-set-name set-separator)
(-> (split-token-set-path token-set-name)
(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 set-name->set-path-string [set-name]
(-> (split-token-set-name set-name)
(join-set-path)))
(defn set-path->set-name [set-path]
(->> (split-token-set-path set-path)
(map (fn [path-part]
(or (-> (split-set-prefix path-part)
(second))
path-part)))
(join-set-path)))
(defn tokens-tree
"Convert tokens into a nested tree with their `:name` as the path.
Optionally use `update-token-fn` option to transform the token."
@ -234,16 +257,27 @@
{: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")
(get-token [_ token-name] "return token by token-name")
(get-tokens [_] "return an ordered sequence of all tokens in the set")
(get-set-path [_] "returns name of set converted to the path with prefix identifiers")
(get-tokens-tree [_] "returns a tree of tokens split & nested by their name path")
(get-dtcg-tokens-tree [_] "returns tokens tree formated to the dtcg spec"))
(defrecord TokenSet [name description modified-at tokens]
ITokenSet
(update-name [_ set-name]
(TokenSet. (-> (split-token-set-path name)
(drop-last)
(concat [set-name])
(join-set-path))
description
(dt/now)
tokens))
(add-token [_ token]
(dm/assert! "expected valid token" (check-token! token))
(TokenSet. name
@ -278,6 +312,9 @@
(get-tokens [_]
(vals tokens))
(get-set-path [_]
(set-name->set-path-string name))
(get-tokens-tree [_]
(tokens-tree tokens))
@ -325,11 +362,14 @@
(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")
(delete-set [_ set-name] "delete a set in the library")
(delete-set-path [_ set-path] "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.
When `before-set-name` is nil, move set to bottom")
(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-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`"))
@ -381,6 +421,8 @@ When `before-set-name` is nil, move set to bottom")
(set-sets [_ set-names] "set the active token sets")
(disable-set [_ set-name] "disable set in theme")
(toggle-set [_ set-name] "toggle a set enabled / disabled in the theme")
(update-set-name [_ prev-set-name set-name] "update set-name from `prev-set-name` to `set-name` when it exists")
(theme-path [_] "get `token-theme-path` from theme")
(theme-matches-group-name [_ group name] "if a theme matches the given group & name")
(hidden-temporary-theme? [_] "if a theme is the (from the user ui) hidden temporary theme"))
@ -403,6 +445,16 @@ When `before-set-name` is nil, move set to bottom")
(disj sets set-name)
(conj sets set-name))))
(update-set-name [this prev-set-name set-name]
(if (get sets prev-set-name)
(TokenTheme. name
group
description
is-source
(dt/now)
(conj (disj sets prev-set-name) set-name))
this))
(theme-path [_]
(token-theme-path group name))
@ -569,36 +621,49 @@ When `before-set-name` is nil, move set to bottom")
(add-sets [this token-sets]
(reduce
(fn [lib set]
(add-set lib set))
this token-sets))
(fn [lib set]
(add-set lib set))
this token-sets))
(update-set [this set-name f]
(let [path (split-token-set-name set-name)
set (get-in sets path)]
(if set
(let [set' (-> (make-token-set (f set))
(assoc :modified-at (dt/now)))
path' (get-token-set-path set')]
(let [set' (-> (make-token-set (f set))
(assoc :modified-at (dt/now)))
path' (get-token-set-path set')
name-changed? (not= (:name set) (:name set'))]
(check-token-set! set')
(TokensLib. (if (= (:name set) (:name set'))
(d/oassoc-in sets path set')
(-> sets
(if name-changed?
(TokensLib. (-> sets
(d/oassoc-in-before path path' set')
(d/dissoc-in path)))
themes
active-themes))
(d/dissoc-in path))
(walk/postwalk
(fn [form]
(if (instance? TokenTheme form)
(update-set-name form (:name set) (:name set'))
form))
themes)
active-themes)
(TokensLib. (d/oassoc-in sets path set')
themes
active-themes)))
this)))
(delete-set [_ set-name]
(let [path (split-token-set-name set-name)]
(delete-set-path [_ set-path]
(let [path (split-token-set-path set-path)
set-node (get-in sets path)
set-group? (not (instance? TokenSet set-node))]
(TokensLib. (d/dissoc-in sets path)
(walk/postwalk
(fn [form]
(if (instance? TokenTheme form)
(disable-set form set-name)
form))
themes)
;; TODO: When deleting a set-group, also deactivate the child sets
(if set-group?
themes
(walk/postwalk
(fn [form]
(if (instance? TokenTheme form)
(disable-set form set-path)
form))
themes))
active-themes)))
;; TODO Handle groups and nesting
@ -620,10 +685,18 @@ When `before-set-name` is nil, move set to bottom")
(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 [_ path]
(some->> (get-in sets (split-token-set-path path))
(tree-seq d/ordered-map? vals)
(filter (partial instance? TokenSet))))
(get-ordered-set-names [this]
(map :name (get-sets this)))