mirror of
https://github.com/penpot/penpot.git
synced 2025-01-21 14:12:36 -05:00
Allow removing other attributes with apply-token function
This commit is contained in:
parent
91033d6dea
commit
f731a30f81
2 changed files with 46 additions and 4 deletions
|
@ -24,7 +24,8 @@
|
||||||
[beicon.v2.core :as rx]
|
[beicon.v2.core :as rx]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
[potok.v2.core :as ptk]
|
[potok.v2.core :as ptk]
|
||||||
[promesa.core :as p]))
|
[promesa.core :as p]
|
||||||
|
[cljs.pprint :as pprint]))
|
||||||
|
|
||||||
;; Helpers ---------------------------------------------------------------------
|
;; Helpers ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -107,7 +108,12 @@
|
||||||
;; Events ----------------------------------------------------------------------
|
;; Events ----------------------------------------------------------------------
|
||||||
|
|
||||||
(defn apply-token
|
(defn apply-token
|
||||||
[{:keys [attributes shape-ids token on-update-shape] :as _props}]
|
"Apply `attributes` that match `token` for `shape-ids`.
|
||||||
|
|
||||||
|
Optionally remove attributes from `attributes-to-remove`,
|
||||||
|
this is useful for applying a single attribute from an attributes set
|
||||||
|
while removing other applied tokens from this set."
|
||||||
|
[{:keys [attributes attributes-to-remove token shape-ids on-update-shape] :as _props}]
|
||||||
(ptk/reify ::apply-token
|
(ptk/reify ::apply-token
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
(watch [_ state _]
|
(watch [_ state _]
|
||||||
|
@ -121,7 +127,9 @@
|
||||||
(rx/of
|
(rx/of
|
||||||
(dwu/start-undo-transaction undo-id)
|
(dwu/start-undo-transaction undo-id)
|
||||||
(dch/update-shapes shape-ids (fn [shape]
|
(dch/update-shapes shape-ids (fn [shape]
|
||||||
(update shape :applied-tokens merge tokenized-attributes)))
|
(cond-> shape
|
||||||
|
attributes-to-remove (update :applied-tokens #(apply (partial dissoc %) attributes-to-remove))
|
||||||
|
:always (update :applied-tokens merge tokenized-attributes))))
|
||||||
(when on-update-shape
|
(when on-update-shape
|
||||||
(on-update-shape resolved-value shape-ids attributes))
|
(on-update-shape resolved-value shape-ids attributes))
|
||||||
(dwu/commit-undo-transaction undo-id)))))))))
|
(dwu/commit-undo-transaction undo-id)))))))))
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
:type :border-radius})))
|
:type :border-radius})))
|
||||||
|
|
||||||
(t/deftest test-apply-token
|
(t/deftest test-apply-token
|
||||||
(t/testing "applying a token twice with the same attributes will override"
|
(t/testing "applying a token twice with the same attributes will override the previous applied token"
|
||||||
(t/async
|
(t/async
|
||||||
done
|
done
|
||||||
(let [file (setup-file)
|
(let [file (setup-file)
|
||||||
|
@ -53,6 +53,40 @@
|
||||||
(t/is (= (:rx rect-1') 24))
|
(t/is (= (:rx rect-1') 24))
|
||||||
(t/is (= (:ry rect-1') 24)))))))))
|
(t/is (= (:ry rect-1') 24)))))))))
|
||||||
|
|
||||||
|
(t/deftest test-apply-token-overwrite
|
||||||
|
(t/testing "removes old token attributes and applies only single attribute"
|
||||||
|
(t/async
|
||||||
|
done
|
||||||
|
(let [file (setup-file)
|
||||||
|
store (ths/setup-store file)
|
||||||
|
rect-1 (cths/get-shape file :rect-1)
|
||||||
|
events [;; Apply `:token-1` to all border radius attributes
|
||||||
|
(wtc/apply-token {:attributes #{:rx :ry :r1 :r2 :r3 :r4}
|
||||||
|
:token (toht/get-token file :token-1)
|
||||||
|
:shape-ids [(:id rect-1)]
|
||||||
|
:on-update-shape wtc/update-shape-radius-all})
|
||||||
|
;; Apply single `:r1` attribute to same shape
|
||||||
|
;; while removing other attributes from the border-radius set
|
||||||
|
;; but keep `:r4` for testing purposes
|
||||||
|
(wtc/apply-token {:attributes #{:r1}
|
||||||
|
:attributes-to-remove #{:rx :ry :r1 :r2 :r3}
|
||||||
|
:token (toht/get-token file :token-2)
|
||||||
|
:shape-ids [(:id rect-1)]
|
||||||
|
:on-update-shape wtc/update-shape-radius-all})]]
|
||||||
|
(tohs/run-store-async
|
||||||
|
store done events
|
||||||
|
(fn [new-state]
|
||||||
|
(let [file' (ths/get-file-from-store new-state)
|
||||||
|
token-1' (toht/get-token file' :token-1)
|
||||||
|
token-2' (toht/get-token file' :token-2)
|
||||||
|
rect-1' (cths/get-shape file' :rect-1)]
|
||||||
|
(t/testing "other border-radius attributes got removed"
|
||||||
|
(t/is (nil? (:rx (:applied-tokens rect-1')))))
|
||||||
|
(t/testing "r1 got applied with :token-2"
|
||||||
|
(t/is (= (:r1 (:applied-tokens rect-1')) (:id token-2'))))
|
||||||
|
(t/testing "while :r4 was kept"
|
||||||
|
(t/is (= (:r4 (:applied-tokens rect-1')) (:id token-1')))))))))));)))))))))))
|
||||||
|
|
||||||
(t/deftest test-apply-border-radius
|
(t/deftest test-apply-border-radius
|
||||||
(t/testing "applies radius token and updates the shapes radius"
|
(t/testing "applies radius token and updates the shapes radius"
|
||||||
(t/async
|
(t/async
|
||||||
|
|
Loading…
Add table
Reference in a new issue