mirror of
https://github.com/penpot/penpot.git
synced 2025-05-10 20:16:37 +02:00
♻️ Simplifies RPC pattern for token themes (#6052)
* ♻️ Add set token theme method schema * ♻️ Add `:set-token-theme` dispatcher for `process-change` multimenthod * ♻️ Add `set-token-theme` to the changes builder * ♻️ Use new method on the frontend * ♻️ Remove unused token theme methods * ♻️ Add tests * ♻️ Add library data to changes * ♻️ Add new test case * ♻️ Remove unused binding
This commit is contained in:
parent
474cd1e55a
commit
b9df8ad038
5 changed files with 179 additions and 93 deletions
|
@ -376,24 +376,6 @@
|
|||
[:type [:= :update-active-token-themes]]
|
||||
[:theme-ids [:set :string]]]]
|
||||
|
||||
[:add-token-theme
|
||||
[:map {:title "AddTokenThemeChange"}
|
||||
[:type [:= :add-token-theme]]
|
||||
[:token-theme ::ctot/token-theme]]]
|
||||
|
||||
[:mod-token-theme
|
||||
[:map {:title "ModTokenThemeChange"}
|
||||
[:type [:= :mod-token-theme]]
|
||||
[:group :string]
|
||||
[:name :string]
|
||||
[:token-theme ::ctot/token-theme]]]
|
||||
|
||||
[:del-token-theme
|
||||
[:map {:title "DelTokenThemeChange"}
|
||||
[:type [:= :del-token-theme]]
|
||||
[:group :string]
|
||||
[:name :string]]]
|
||||
|
||||
[:add-token-sets
|
||||
[:map {:title "AddTokenSetsChange"}
|
||||
[:type [:= :add-token-sets]]
|
||||
|
@ -422,6 +404,13 @@
|
|||
[:before-path [:maybe [:vector :string]]]
|
||||
[:before-group? [:maybe :boolean]]]]
|
||||
|
||||
[:set-token-theme
|
||||
[:map {:title "SetTokenThemeChange"}
|
||||
[:type [:= :set-token-theme]]
|
||||
[:theme-name :string]
|
||||
[:group :string]
|
||||
[:theme [:maybe ::ctot/token-theme]]]]
|
||||
|
||||
[:set-tokens-lib
|
||||
[:map {:title "SetTokensLib"}
|
||||
[:type [:= :set-tokens-lib]]
|
||||
|
@ -1038,32 +1027,29 @@
|
|||
(ctob/update-set lib' set-name (fn [prev-token-set]
|
||||
(ctob/make-token-set (merge prev-token-set token-set)))))))))
|
||||
|
||||
(defmethod process-change :set-token-theme
|
||||
[data {:keys [group theme-name theme]}]
|
||||
(update data :tokens-lib
|
||||
(fn [lib]
|
||||
(let [lib' (ctob/ensure-tokens-lib lib)]
|
||||
(cond
|
||||
(not theme)
|
||||
(ctob/delete-theme lib' group theme-name)
|
||||
|
||||
(not (ctob/get-theme lib' group theme-name))
|
||||
(ctob/add-theme lib' (ctob/make-token-theme theme))
|
||||
|
||||
:else
|
||||
(ctob/update-theme lib'
|
||||
group theme-name
|
||||
(fn [prev-token-theme]
|
||||
(ctob/make-token-theme (merge prev-token-theme theme)))))))))
|
||||
|
||||
(defmethod process-change :update-active-token-themes
|
||||
[data {:keys [theme-ids]}]
|
||||
(update data :tokens-lib #(-> % (ctob/ensure-tokens-lib)
|
||||
(ctob/set-active-themes theme-ids))))
|
||||
|
||||
(defmethod process-change :add-token-theme
|
||||
[data {:keys [token-theme]}]
|
||||
(update data :tokens-lib #(-> %
|
||||
(ctob/ensure-tokens-lib)
|
||||
(ctob/add-theme (-> token-theme
|
||||
(ctob/make-token-theme))))))
|
||||
|
||||
(defmethod process-change :mod-token-theme
|
||||
[data {:keys [name group token-theme]}]
|
||||
(update data :tokens-lib #(-> %
|
||||
(ctob/ensure-tokens-lib)
|
||||
(ctob/update-theme group name
|
||||
(fn [prev-theme]
|
||||
(merge prev-theme token-theme))))))
|
||||
|
||||
(defmethod process-change :del-token-theme
|
||||
[data {:keys [group name]}]
|
||||
(update data :tokens-lib #(-> %
|
||||
(ctob/ensure-tokens-lib)
|
||||
(ctob/delete-theme group name))))
|
||||
|
||||
(defmethod process-change :add-token-sets
|
||||
[data {:keys [token-sets]}]
|
||||
(update data :tokens-lib #(-> %
|
||||
|
|
|
@ -774,33 +774,30 @@
|
|||
(update :undo-changes conj {:type :update-active-token-themes :theme-ids prev-token-active-theme-ids})
|
||||
(apply-changes-local)))
|
||||
|
||||
(defn add-token-theme
|
||||
[changes token-theme]
|
||||
(-> changes
|
||||
(update :redo-changes conj {:type :add-token-theme :token-theme token-theme})
|
||||
(update :undo-changes conj {:type :del-token-theme :group (:group token-theme) :name (:name token-theme)})
|
||||
(apply-changes-local)))
|
||||
|
||||
(defn update-token-theme
|
||||
[changes token-theme prev-token-theme]
|
||||
(let [name (or (:name prev-token-theme)
|
||||
(:name token-theme))
|
||||
group (or (:group prev-token-theme)
|
||||
(:group token-theme))]
|
||||
(-> changes
|
||||
(update :redo-changes conj {:type :mod-token-theme :group group :name name :token-theme token-theme})
|
||||
(update :undo-changes conj {:type :mod-token-theme :group group :name name :token-theme (or prev-token-theme token-theme)})
|
||||
(apply-changes-local))))
|
||||
|
||||
(defn delete-token-theme
|
||||
[changes group name]
|
||||
(defn set-token-theme [changes group theme-name theme]
|
||||
(assert-library! changes)
|
||||
(let [library-data (::library-data (meta changes))
|
||||
prev-token-theme (some-> (get library-data :tokens-lib)
|
||||
(ctob/get-theme group name))]
|
||||
prev-theme (some-> (get library-data :tokens-lib)
|
||||
(ctob/get-theme group theme-name))]
|
||||
(-> changes
|
||||
(update :redo-changes conj {:type :del-token-theme :group group :name name})
|
||||
(update :undo-changes conj {:type :add-token-theme :token-theme prev-token-theme})
|
||||
(update :redo-changes conj {:type :set-token-theme
|
||||
:theme-name theme-name
|
||||
:group group
|
||||
:theme theme})
|
||||
(update :undo-changes conj (if prev-theme
|
||||
{:type :set-token-theme
|
||||
:group group
|
||||
:theme-name (or
|
||||
;; Undo of edit
|
||||
(:name theme)
|
||||
;; Undo of delete
|
||||
theme-name)
|
||||
:theme prev-theme}
|
||||
;; Undo of create
|
||||
{:type :set-token-theme
|
||||
:group group
|
||||
:theme-name theme-name
|
||||
:theme nil}))
|
||||
(apply-changes-local))))
|
||||
|
||||
(defn rename-token-set-group
|
||||
|
|
|
@ -13,13 +13,12 @@
|
|||
prev-hidden-token-theme (ctob/get-hidden-theme tokens-lib)
|
||||
|
||||
hidden-token-theme (-> (some-> prev-hidden-token-theme (ctob/set-sets active-token-set-names))
|
||||
(update-theme-fn))
|
||||
|
||||
changes (-> changes
|
||||
(pcb/update-active-token-themes #{ctob/hidden-token-theme-path} prev-active-token-themes))
|
||||
|
||||
changes (pcb/update-token-theme changes hidden-token-theme prev-hidden-token-theme)]
|
||||
changes))
|
||||
(update-theme-fn))]
|
||||
(-> changes
|
||||
(pcb/update-active-token-themes #{ctob/hidden-token-theme-path} prev-active-token-themes)
|
||||
(pcb/set-token-theme (:group prev-hidden-token-theme)
|
||||
(:name prev-hidden-token-theme)
|
||||
hidden-token-theme))))
|
||||
|
||||
(defn generate-toggle-token-set
|
||||
"Toggle a token set at `set-name` in `tokens-lib` without modifying a user theme."
|
||||
|
|
|
@ -22,7 +22,9 @@
|
|||
(ctob/add-theme (ctob/make-token-theme :name "theme"
|
||||
:sets #{"foo/bar"}))
|
||||
(ctob/set-active-themes #{"/theme"})))
|
||||
changes (clt/generate-toggle-token-set (pcb/empty-changes) (tht/get-tokens-lib file) "foo/bar")
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data (:data file))
|
||||
(clt/generate-toggle-token-set (tht/get-tokens-lib file) "foo/bar"))
|
||||
|
||||
redo (thf/apply-changes file changes)
|
||||
redo-lib (tht/get-tokens-lib redo)
|
||||
|
@ -40,7 +42,9 @@
|
|||
(ctob/add-theme (ctob/make-token-theme :name "theme"
|
||||
:sets #{"foo/bar"}))
|
||||
(ctob/set-active-themes #{"/theme"})))
|
||||
changes (clt/generate-toggle-token-set (pcb/empty-changes) (tht/get-tokens-lib file) "foo/bar")
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data (:data file))
|
||||
(clt/generate-toggle-token-set (tht/get-tokens-lib file) "foo/bar"))
|
||||
|
||||
redo (thf/apply-changes file changes)
|
||||
redo-lib (tht/get-tokens-lib redo)
|
||||
|
@ -58,7 +62,9 @@
|
|||
(ctob/add-theme (ctob/make-hidden-token-theme))
|
||||
(ctob/set-active-themes #{ctob/hidden-token-theme-path})))
|
||||
|
||||
changes (clt/generate-toggle-token-set-group (pcb/empty-changes) (tht/get-tokens-lib file) ["foo"])
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data (:data file))
|
||||
(clt/generate-toggle-token-set-group (tht/get-tokens-lib file) ["foo"]))
|
||||
|
||||
redo (thf/apply-changes file changes)
|
||||
redo-lib (tht/get-tokens-lib redo)
|
||||
|
@ -68,6 +74,87 @@
|
|||
|
||||
(t/is (= #{"foo/bar"} (:sets (ctob/get-hidden-theme redo-lib)))))))
|
||||
|
||||
(t/deftest set-token-theme-test
|
||||
(t/testing "delete token theme"
|
||||
(let [theme-name "foo"
|
||||
group "main"
|
||||
file (setup-file #(-> %
|
||||
(ctob/add-theme (ctob/make-token-theme :name theme-name
|
||||
:group group))))
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data (:data file))
|
||||
(pcb/set-token-theme group theme-name nil))
|
||||
|
||||
redo (thf/apply-changes file changes)
|
||||
redo-lib (tht/get-tokens-lib redo)
|
||||
undo (thf/apply-undo-changes redo changes)
|
||||
undo-lib (tht/get-tokens-lib undo)]
|
||||
;; Redo
|
||||
(t/is (nil? (ctob/get-theme redo-lib group theme-name)))
|
||||
;; Undo
|
||||
(t/is (some? (ctob/get-theme undo-lib group theme-name)))))
|
||||
|
||||
(t/testing "add token theme"
|
||||
(let [theme-name "foo"
|
||||
group "main"
|
||||
theme (ctob/make-token-theme :name theme-name
|
||||
:group group)
|
||||
file (setup-file identity)
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data (:data file))
|
||||
(pcb/set-token-theme group theme-name theme))
|
||||
redo (thf/apply-changes file changes)
|
||||
redo-lib (tht/get-tokens-lib redo)
|
||||
undo (thf/apply-undo-changes redo changes)
|
||||
undo-lib (tht/get-tokens-lib undo)]
|
||||
;; Redo
|
||||
(t/is (some? (ctob/get-theme redo-lib group theme-name)))
|
||||
;; Undo
|
||||
(t/is (nil? (ctob/get-theme undo-lib group theme-name)))))
|
||||
|
||||
(t/testing "update token theme"
|
||||
(let [theme-name "foo"
|
||||
group "main"
|
||||
prev-theme (ctob/make-token-theme :name theme-name
|
||||
:group group)
|
||||
file (setup-file #(ctob/add-theme % prev-theme))
|
||||
new-theme-name "foo1"
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data (:data file))
|
||||
(pcb/set-token-theme group new-theme-name prev-theme))
|
||||
redo (thf/apply-changes file changes)
|
||||
redo-lib (tht/get-tokens-lib redo)
|
||||
undo (thf/apply-undo-changes redo changes)
|
||||
undo-lib (tht/get-tokens-lib undo)]
|
||||
;; Redo
|
||||
(t/is (some? (ctob/get-theme redo-lib group theme-name)))
|
||||
(t/is (nil? (ctob/get-theme redo-lib group new-theme-name)))
|
||||
;; Undo
|
||||
(t/is (some? (ctob/get-theme undo-lib group theme-name)))
|
||||
(t/is (nil? (ctob/get-theme undo-lib group new-theme-name)))))
|
||||
|
||||
(t/testing "toggling token theme updates using changes history"
|
||||
(let [theme-name "foo-theme"
|
||||
group "main"
|
||||
set-name "bar-set"
|
||||
token-set (ctob/make-token-set :name set-name)
|
||||
theme (ctob/make-token-theme :name theme-name
|
||||
:group group)
|
||||
file (setup-file #(-> %
|
||||
(ctob/add-theme theme)
|
||||
(ctob/add-set token-set)))
|
||||
theme' (assoc theme :sets #{set-name})
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data (:data file))
|
||||
(pcb/set-token-theme group theme-name theme'))
|
||||
changed-file (-> file
|
||||
(thf/apply-changes changes)
|
||||
(thf/apply-undo-changes changes)
|
||||
(thf/apply-changes changes))
|
||||
changed-lib (tht/get-tokens-lib changed-file)]
|
||||
(t/is (= #{set-name}
|
||||
(-> changed-lib (ctob/get-theme group theme-name) :sets))))))
|
||||
|
||||
(t/deftest set-token-test
|
||||
(t/testing "delete token"
|
||||
(let [set-name "foo"
|
||||
|
@ -198,7 +285,9 @@
|
|||
(ctob/add-set (ctob/make-token-set :name "foo/bar/baz/baz-child"))
|
||||
(ctob/add-theme (ctob/make-token-theme :name "theme"))
|
||||
(ctob/set-active-themes #{"/theme"})))
|
||||
changes (clt/generate-toggle-token-set-group (pcb/empty-changes) (tht/get-tokens-lib file) ["foo" "bar"])
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data (:data file))
|
||||
(clt/generate-toggle-token-set-group (tht/get-tokens-lib file) ["foo" "bar"]))
|
||||
|
||||
redo (thf/apply-changes file changes)
|
||||
redo-lib (tht/get-tokens-lib redo)
|
||||
|
@ -219,7 +308,9 @@
|
|||
:sets #{"foo/bar/baz"}))
|
||||
(ctob/set-active-themes #{"/theme"})))
|
||||
|
||||
changes (clt/generate-toggle-token-set-group (pcb/empty-changes) (tht/get-tokens-lib file) ["foo" "bar"])
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data (:data file))
|
||||
(clt/generate-toggle-token-set-group (tht/get-tokens-lib file) ["foo" "bar"]))
|
||||
|
||||
redo (thf/apply-changes file changes)
|
||||
redo-lib (tht/get-tokens-lib redo)
|
||||
|
|
|
@ -70,9 +70,13 @@
|
|||
(let [new-token-theme token-theme]
|
||||
(ptk/reify ::create-token-theme
|
||||
ptk/WatchEvent
|
||||
(watch [it _ _]
|
||||
(let [changes (-> (pcb/empty-changes it)
|
||||
(pcb/add-token-theme new-token-theme))]
|
||||
(watch [it state _]
|
||||
(let [data (dsh/lookup-file-data state)
|
||||
changes (-> (pcb/empty-changes it)
|
||||
(pcb/with-library-data data)
|
||||
(pcb/set-token-theme (:group new-token-theme)
|
||||
(:name new-token-theme)
|
||||
new-token-theme))]
|
||||
(rx/of
|
||||
(dch/commit-changes changes)))))))
|
||||
|
||||
|
@ -81,8 +85,13 @@
|
|||
ptk/WatchEvent
|
||||
(watch [it state _]
|
||||
(let [tokens-lib (get-tokens-lib state)
|
||||
data (dsh/lookup-file-data state)
|
||||
prev-token-theme (some-> tokens-lib (ctob/get-theme group name))
|
||||
changes (pcb/update-token-theme (pcb/empty-changes it) token-theme prev-token-theme)]
|
||||
changes (-> (pcb/empty-changes it)
|
||||
(pcb/with-library-data data)
|
||||
(pcb/set-token-theme (:group prev-token-theme)
|
||||
(:name prev-token-theme)
|
||||
token-theme))]
|
||||
(rx/of
|
||||
(dch/commit-changes changes))))))
|
||||
|
||||
|
@ -105,15 +114,14 @@
|
|||
(dch/commit-changes changes)
|
||||
(wtu/update-workspace-tokens))))))
|
||||
|
||||
(defn delete-token-theme [group name]
|
||||
(defn delete-token-theme [group theme-name]
|
||||
(ptk/reify ::delete-token-theme
|
||||
ptk/WatchEvent
|
||||
(watch [it state _]
|
||||
(let [data (dsh/lookup-file-data state)
|
||||
|
||||
changes (-> (pcb/empty-changes it)
|
||||
(pcb/with-library-data data)
|
||||
(pcb/delete-token-theme group name))]
|
||||
(pcb/set-token-theme group theme-name nil))]
|
||||
(rx/of
|
||||
(dch/commit-changes changes)
|
||||
(wtu/update-workspace-tokens))))))
|
||||
|
@ -167,8 +175,10 @@
|
|||
(ptk/reify ::toggle-token-set
|
||||
ptk/WatchEvent
|
||||
(watch [_ state _]
|
||||
(let [tlib (get-tokens-lib state)
|
||||
(let [data (dsh/lookup-file-data state)
|
||||
tlib (get-tokens-lib state)
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data data)
|
||||
(clt/generate-toggle-token-set tlib name))]
|
||||
|
||||
(rx/of (dch/commit-changes changes)
|
||||
|
@ -178,7 +188,10 @@
|
|||
(ptk/reify ::toggle-token-set-group
|
||||
ptk/WatchEvent
|
||||
(watch [_ state _]
|
||||
(let [changes (clt/generate-toggle-token-set-group (pcb/empty-changes) (get-tokens-lib state) group-path)]
|
||||
(let [data (dsh/lookup-file-data state)
|
||||
changes (-> (pcb/empty-changes)
|
||||
(pcb/with-library-data data)
|
||||
(clt/generate-toggle-token-set-group (get-tokens-lib state) group-path))]
|
||||
(rx/of
|
||||
(dch/commit-changes changes)
|
||||
(wtu/update-workspace-tokens))))))
|
||||
|
@ -252,9 +265,11 @@
|
|||
(ptk/reify ::create-token-and-set
|
||||
ptk/WatchEvent
|
||||
(watch [_ state _]
|
||||
(let [set-name "Global"
|
||||
(let [data
|
||||
(dsh/lookup-file-data state)
|
||||
|
||||
data (dsh/lookup-file-data state)
|
||||
set-name
|
||||
"Global"
|
||||
|
||||
token-set
|
||||
(-> (ctob/make-token-set :name set-name)
|
||||
|
@ -269,13 +284,11 @@
|
|||
changes
|
||||
(-> (pcb/empty-changes)
|
||||
(pcb/with-library-data data)
|
||||
(pcb/set-token-set set-name false token-set))
|
||||
|
||||
changes
|
||||
(-> changes
|
||||
(pcb/update-token-theme hidden-theme-with-set hidden-theme)
|
||||
(pcb/set-token-set set-name false token-set)
|
||||
(pcb/set-token-theme (:group hidden-theme)
|
||||
(:name hidden-theme)
|
||||
hidden-theme-with-set)
|
||||
(pcb/update-active-token-themes #{ctob/hidden-token-theme-path} #{}))]
|
||||
|
||||
(rx/of (dch/commit-changes changes)
|
||||
(set-selected-token-set-name set-name))))))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue