Improve code convetion related to changes protocol

Partial work, still pending to make changes to other related
changes definitions
This commit is contained in:
Andrey Antukh 2025-03-19 12:52:03 +01:00
parent 39a1d5cc89
commit b6bb93f0b6
7 changed files with 115 additions and 77 deletions

View file

@ -382,13 +382,13 @@
[:set-group-path [:vector :string]] [:set-group-path [:vector :string]]
[:set-group-fname :string]]] [:set-group-fname :string]]]
[:move-token-set-before [:move-token-set
[:map {:title "MoveTokenSetBefore"} [:map {:title "MoveTokenSet"}
[:type [:= :move-token-set-before]] [:type [:= :move-token-set]]
[:from-path [:vector :string]] [:from-path [:vector :string]]
[:to-path [:vector :string]] [:to-path [:vector :string]]
[:before-path [:maybe [:vector :string]]] [:before-path [:maybe [:vector :string]]]
[:before-group? [:maybe :boolean]]]] [:before-group [:maybe :boolean]]]]
[:move-token-set-group-before [:move-token-set-group-before
[:map {:title "MoveTokenSetGroupBefore"} [:map {:title "MoveTokenSetGroupBefore"}
@ -1051,11 +1051,11 @@
(ctob/ensure-tokens-lib) (ctob/ensure-tokens-lib)
(ctob/rename-set-group set-group-path set-group-fname))))) (ctob/rename-set-group set-group-path set-group-fname)))))
(defmethod process-change :move-token-set-before (defmethod process-change :move-token-set
[data {:keys [from-path to-path before-path before-group?] :as changes}] [data {:keys [from-path to-path before-path before-group] :as changes}]
(update data :tokens-lib #(-> % (update data :tokens-lib #(-> %
(ctob/ensure-tokens-lib) (ctob/ensure-tokens-lib)
(ctob/move-set from-path to-path before-path before-group?)))) (ctob/move-set from-path to-path before-path before-group))))
(defmethod process-change :move-token-set-group-before (defmethod process-change :move-token-set-group-before
[data {:keys [from-path to-path before-path before-group?]}] [data {:keys [from-path to-path before-path before-group?]}]

View file

@ -809,19 +809,19 @@
(update :undo-changes conj {:type :rename-token-set-group :set-group-path undo-path :set-group-fname undo-fname}) (update :undo-changes conj {:type :rename-token-set-group :set-group-path undo-path :set-group-fname undo-fname})
(apply-changes-local)))) (apply-changes-local))))
(defn move-token-set-before (defn move-token-set
[changes {:keys [from-path to-path before-path before-group? prev-before-path prev-before-group?] :as opts}] [changes {:keys [from-path to-path before-path before-group? prev-before-path prev-before-group?] :as opts}]
(-> changes (-> changes
(update :redo-changes conj {:type :move-token-set-before (update :redo-changes conj {:type :move-token-set
:from-path from-path :from-path from-path
:to-path to-path :to-path to-path
:before-path before-path :before-path before-path
:before-group? before-group?}) :before-group before-group?})
(update :undo-changes conj {:type :move-token-set-before (update :undo-changes conj {:type :move-token-set
:from-path to-path :from-path to-path
:to-path from-path :to-path from-path
:before-path prev-before-path :before-path prev-before-path
:before-group? prev-before-group?}) :before-group prev-before-group?})
(apply-changes-local))) (apply-changes-local)))
(defn move-token-set-group-before (defn move-token-set-group-before

View file

@ -1,11 +1,21 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns app.common.logic.tokens (ns app.common.logic.tokens
(:require (:require
[app.common.files.changes-builder :as pcb] [app.common.files.changes-builder :as pcb]
[app.common.types.tokens-lib :as ctob])) [app.common.types.tokens-lib :as ctob]))
(defn generate-update-active-sets (defn generate-update-active-sets
"Copy the active sets from the currently active themes and move them to the hidden token theme and update the theme with `update-theme-fn`. "Copy the active sets from the currently active themes and move them
Use this for managing sets active state without having to modify a user created theme (\"no themes selected\" state in the ui)." to the hidden token theme and update the theme with
`update-theme-fn`.
Use this for managing sets active state without having to modify a
user created theme (\"no themes selected\" state in the ui)."
[changes tokens-lib update-theme-fn] [changes tokens-lib update-theme-fn]
(let [prev-active-token-themes (ctob/get-active-theme-paths tokens-lib) (let [prev-active-token-themes (ctob/get-active-theme-paths tokens-lib)
active-token-set-names (ctob/get-active-themes-set-names tokens-lib) active-token-set-names (ctob/get-active-themes-set-names tokens-lib)
@ -21,7 +31,8 @@
hidden-token-theme)))) hidden-token-theme))))
(defn generate-toggle-token-set (defn generate-toggle-token-set
"Toggle a token set at `set-name` in `tokens-lib` without modifying a user theme." "Toggle a token set at `set-name` in `tokens-lib` without modifying a
user theme."
[changes tokens-lib set-name] [changes tokens-lib set-name]
(generate-update-active-sets changes tokens-lib #(ctob/toggle-set % set-name))) (generate-update-active-sets changes tokens-lib #(ctob/toggle-set % set-name)))
@ -121,9 +132,9 @@
(defn generate-move-token-set (defn generate-move-token-set
"Create changes for dropping a token set or token set. "Create changes for dropping a token set or token set.
Throws for impossible moves." Throws for impossible moves."
[changes tokens-lib drop-opts] [changes tokens-lib params]
(if-let [drop-opts' (calculate-move-token-set-or-set-group tokens-lib drop-opts)] (if-let [params (calculate-move-token-set-or-set-group tokens-lib params)]
(pcb/move-token-set-before changes drop-opts') (pcb/move-token-set changes params)
changes)) changes))
(defn generate-move-token-set-group (defn generate-move-token-set-group

View file

@ -945,14 +945,21 @@ Will return a value that matches this schema:
(let [prefixed-from-path (set-full-path->set-prefixed-full-path from-path) (let [prefixed-from-path (set-full-path->set-prefixed-full-path from-path)
prev-set (get-in sets prefixed-from-path)] prev-set (get-in sets prefixed-from-path)]
(if (instance? TokenSet prev-set) (if (instance? TokenSet prev-set)
(let [prefixed-to-path (set-full-path->set-prefixed-full-path to-path) (let [prefixed-to-path
prefixed-before-path (when before-path (set-full-path->set-prefixed-full-path to-path)
prefixed-before-path
(when before-path
(if before-group? (if before-group?
(mapv add-set-path-group-prefix before-path) (mapv add-set-path-group-prefix before-path)
(set-full-path->set-prefixed-full-path before-path))) (set-full-path->set-prefixed-full-path before-path)))
set (assoc prev-set :name (join-set-path to-path)) set
reorder? (= prefixed-from-path prefixed-to-path) (assoc prev-set :name (join-set-path to-path))
reorder?
(= prefixed-from-path prefixed-to-path)
sets' sets'
(if reorder? (if reorder?
(d/oreorder-before sets (d/oreorder-before sets
@ -964,6 +971,7 @@ Will return a value that matches this schema:
(d/oassoc-in-before sets prefixed-before-path prefixed-to-path set) (d/oassoc-in-before sets prefixed-before-path prefixed-to-path set)
(d/oassoc-in sets prefixed-to-path set)) (d/oassoc-in sets prefixed-to-path set))
(d/dissoc-in prefixed-from-path)))] (d/dissoc-in prefixed-from-path)))]
(TokensLib. sets' (TokensLib. sets'
(if reorder? (if reorder?
themes themes

View file

@ -84,8 +84,7 @@
(t/is (thrown-with-msg? #?(:cljs js/Error :clj Exception) #"expected valid params for token-set" (t/is (thrown-with-msg? #?(:cljs js/Error :clj Exception) #"expected valid params for token-set"
(ctob/make-token-set params))))) (ctob/make-token-set params)))))
(t/deftest move-token-set (t/deftest move-token-set-flat
(t/testing "flat"
(let [tokens-lib (-> (ctob/make-tokens-lib) (let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :name "A")) (ctob/add-set (ctob/make-token-set :name "A"))
(ctob/add-set (ctob/make-token-set :name "B")) (ctob/add-set (ctob/make-token-set :name "B"))
@ -103,7 +102,7 @@
(t/testing "move to bottom" (t/testing "move to bottom"
(t/is (= ["A" "B" "Move"] (move ["Move"] ["Move"] nil false)))))) (t/is (= ["A" "B" "Move"] (move ["Move"] ["Move"] nil false))))))
(t/testing "nested" (t/deftest move-token-set-nested
(let [tokens-lib (-> (ctob/make-tokens-lib) (let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :name "Foo/Baz")) (ctob/add-set (ctob/make-token-set :name "Foo/Baz"))
(ctob/add-set (ctob/make-token-set :name "Foo/Bar")) (ctob/add-set (ctob/make-token-set :name "Foo/Bar"))
@ -121,15 +120,29 @@
(t/is (= ["Foo/Foo" "Foo/Baz" "Foo/Bar"] (move ["Foo"] ["Foo" "Foo"] ["Foo" "Baz"] false))) (t/is (= ["Foo/Foo" "Foo/Baz" "Foo/Bar"] (move ["Foo"] ["Foo" "Foo"] ["Foo" "Baz"] false)))
(t/is (= ["Foo/Baz" "Foo/Bar" "Foo/Foo"] (move ["Foo"] ["Foo" "Foo"] nil false)))))) (t/is (= ["Foo/Baz" "Foo/Bar" "Foo/Foo"] (move ["Foo"] ["Foo" "Foo"] nil false))))))
;; FIXME
(t/testing "updates theme set names" (t/deftest move-token-set-nested-2
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :name "a/b"))
(ctob/add-set (ctob/make-token-set :name "a/a"))
(ctob/add-set (ctob/make-token-set :name "b/a"))
(ctob/add-set (ctob/make-token-set :name "b/b")))
move (fn [from-path to-path before-path before-group?]
(->> (ctob/move-set tokens-lib from-path to-path before-path before-group?)
(ctob/get-ordered-set-names)
(vec)))]
(t/testing "move within group"
(t/is (= ["a/b" "a/a" "b/a" "b/b"] (vec (ctob/get-ordered-set-names tokens-lib))))
(t/is (= ["a/a" "a/b" "b/a" "b/b"] (move ["a" "b"] ["a" "b"] nil true))))))
(t/deftest move-token-set-nested-3
(let [tokens-lib (-> (ctob/make-tokens-lib) (let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :name "Foo/Bar/Baz")) (ctob/add-set (ctob/make-token-set :name "Foo/Bar/Baz"))
(ctob/add-set (ctob/make-token-set :name "Other")) (ctob/add-set (ctob/make-token-set :name "Other"))
(ctob/add-theme (ctob/make-token-theme :name "Theme" (ctob/add-theme (ctob/make-token-theme :name "Theme"
:sets #{"Foo/Bar/Baz"})) :sets #{"Foo/Bar/Baz"}))
(ctob/move-set ["Foo" "Bar" "Baz"] ["Other/Baz"] nil nil))] (ctob/move-set ["Foo" "Bar" "Baz"] ["Other/Baz"] nil nil))]
(t/is (= #{"Other/Baz"} (:sets (ctob/get-theme tokens-lib "" "Theme"))))))) (t/is (= #{"Other/Baz"} (:sets (ctob/get-theme tokens-lib "" "Theme"))))))
(t/deftest move-token-set-group (t/deftest move-token-set-group
(t/testing "reordering" (t/testing "reordering"

View file

@ -252,6 +252,8 @@
:level :error :level :error
:timeout 9000}))))))) :timeout 9000})))))))
;; FIXME: add schema for params
(defn drop-token-set-group [drop-opts] (defn drop-token-set-group [drop-opts]
(ptk/reify ::drop-token-set-group (ptk/reify ::drop-token-set-group
ptk/WatchEvent ptk/WatchEvent
@ -265,17 +267,21 @@
(rx/of (rx/of
(drop-error (ex-data e)))))))) (drop-error (ex-data e))))))))
(defn drop-token-set [drop-opts] ;; FIXME: add schema for params
(defn drop-token-set
[params]
(ptk/reify ::drop-token-set (ptk/reify ::drop-token-set
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(try (try
(when-let [changes (clt/generate-move-token-set (pcb/empty-changes it) (get-tokens-lib state) drop-opts)] (let [tokens-lib (get-tokens-lib state)
changes (-> (pcb/empty-changes it)
(clt/generate-move-token-set tokens-lib params))]
(rx/of (dch/commit-changes changes) (rx/of (dch/commit-changes changes)
(wtu/update-workspace-tokens))) (wtu/update-workspace-tokens)))
(catch :default e (catch :default cause
(rx/of (rx/of (drop-error (ex-data cause))))))))
(drop-error (ex-data e))))))))
(defn- create-token-with-set (defn- create-token-with-set
"A special case when a first token is created and no set exists" "A special case when a first token is created and no set exists"

View file

@ -368,13 +368,13 @@
(mf/use-fn (mf/use-fn
(mf/deps collapsed-paths) (mf/deps collapsed-paths)
(fn [tree-index position data] (fn [tree-index position data]
(let [props {:from-index (:index data) (let [params {:from-index (:index data)
:to-index tree-index :to-index tree-index
:position position :position position
:collapsed-paths collapsed-paths}] :collapsed-paths collapsed-paths}]
(if (:is-group data) (if (:is-group data)
(st/emit! (dt/drop-token-set-group props)) (st/emit! (dt/drop-token-set-group params))
(st/emit! (dt/drop-token-set props)))))) (st/emit! (dt/drop-token-set params))))))
on-toggle-collapse on-toggle-collapse
(mf/use-fn (mf/use-fn