From f2358b98273cb767f2e0b9b17b381fd3f8e9435a Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Tue, 2 Jul 2024 08:22:27 +0200 Subject: [PATCH] Use toggle function --- .../ui/workspace/tokens/context_menu.cljs | 2 +- .../app/main/ui/workspace/tokens/core.cljs | 43 ++++++++++++++++++- .../app/main/ui/workspace/tokens/sidebar.cljs | 8 ++-- .../app/main/ui/workspace/tokens/token.cljs | 2 +- frontend/test/token_tests/token_test.cljs | 6 +-- 5 files changed, 50 insertions(+), 11 deletions(-) diff --git a/frontend/src/app/main/ui/workspace/tokens/context_menu.cljs b/frontend/src/app/main/ui/workspace/tokens/context_menu.cljs index 6d4c0e073..352268480 100644 --- a/frontend/src/app/main/ui/workspace/tokens/context_menu.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/context_menu.cljs @@ -213,7 +213,7 @@ (defn additional-actions [{:keys [token-id token-type selected-shapes] :as context-data}] (let [attributes->actions (fn [update-fn coll] (for [{:keys [attributes] :as item} coll] - (let [selected? (wtt/tokens-applied? {:id token-id} selected-shapes attributes)] + (let [selected? (wtt/shapes-token-applied? {:id token-id} selected-shapes attributes)] (assoc item :action #(update-fn context-data attributes) :selected? selected?))))] diff --git a/frontend/src/app/main/ui/workspace/tokens/core.cljs b/frontend/src/app/main/ui/workspace/tokens/core.cljs index b4e0c13f2..b375109da 100644 --- a/frontend/src/app/main/ui/workspace/tokens/core.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/core.cljs @@ -21,7 +21,8 @@ [app.util.dom :as dom] [app.util.webapi :as wapi] [cuerdas.core :as str] - [promesa.core :as p])) + [promesa.core :as p] + [clojure.set :as set])) ;; Helpers --------------------------------------------------------------------- @@ -48,13 +49,51 @@ ;; Update functions ------------------------------------------------------------ +(defn apply-tokens? + [{:keys [attributes token shapes] :as _props}] + (let [{:keys [with-token without-token]} (group-by + (fn [shape] + (if (wtt/shapes-token-applied? token shape attributes) + :with-token + :without-token)) + shapes)] + (and (empty? with-token) (seq without-token)))) + +(defn on-add-token [{:keys [token-type-props token shapes] :as _props}] + (p/let [sd-tokens (sd/resolve-workspace-tokens+)] + (let [{:keys [attributes on-update-shape]} token-type-props + shape-ids (map :id shapes) + resolved-value (-> (get sd-tokens (:id token)) + (resolve-token-value)) + tokenized-attributes (->> (map (fn [attr] {attr (:id token)}) attributes) + (into {}))] + (st/emit! + (dch/update-shapes shape-ids (fn [shape] (update shape :applied-tokens merge tokenized-attributes)))) + (on-update-shape resolved-value shape-ids attributes)))) + +(def remove-keys #(apply dissoc %1 %2)) + +(defn on-remove-token [{:keys [token-type-props shapes] :as _props}] + (st/emit! + (dch/update-shapes (map :id shapes) + (fn [shape] + (update shape :applied-tokens remove-keys (:attributes token-type-props)))))) + +(defn on-toggle-token + [{:keys [token-type-props token shapes] :as props}] + (let [remove-tokens? (wtt/shapes-token-applied? token shapes (:attributes token-type-props))] + (if remove-tokens? + (on-remove-token props) + (on-add-token props)))) + (defn on-apply-token [{:keys [token token-type-props selected-shapes] :as _props}] (let [{:keys [attributes on-apply on-update-shape] :or {on-apply dt/update-token-from-attributes}} token-type-props shape-ids (->> selected-shapes (eduction - (remove #(wtt/tokens-applied? token % attributes)) + (remove #(wtt/shapes-token-applied? token % attributes)) (map :id)))] + (p/let [sd-tokens (sd/resolve-workspace-tokens+ {:debug? true})] (let [resolved-token (get sd-tokens (:id token)) resolved-token-value (resolve-token-value resolved-token)] diff --git a/frontend/src/app/main/ui/workspace/tokens/sidebar.cljs b/frontend/src/app/main/ui/workspace/tokens/sidebar.cljs index 3b25a7d78..0addb4fad 100644 --- a/frontend/src/app/main/ui/workspace/tokens/sidebar.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/sidebar.cljs @@ -99,9 +99,9 @@ (mf/deps selected-shapes token-type-props) (fn [event token] (dom/stop-propagation event) - (wtc/on-apply-token {:token token - :token-type-props token-type-props - :selected-shapes selected-shapes}))) + (wtc/on-toggle-token {:token token + :shapes selected-shapes + :token-type-props token-type-props}))) tokens-count (count tokens)] [:div {:on-click on-toggle-open-click} [:& cmm/asset-section {:icon (mf/fnc icon-wrapper [_] @@ -122,7 +122,7 @@ [:& token-pill {:key (:id token) :token token - :highlighted? (wtt/tokens-applied? token selected-shapes attributes) + :highlighted? (wtt/shapes-token-applied? token selected-shapes attributes) :on-click #(on-token-pill-click % token) :on-context-menu #(on-context-menu % token)}])]])]])) diff --git a/frontend/src/app/main/ui/workspace/tokens/token.cljs b/frontend/src/app/main/ui/workspace/tokens/token.cljs index 1750db9f3..e52079f8f 100644 --- a/frontend/src/app/main/ui/workspace/tokens/token.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/token.cljs @@ -11,7 +11,7 @@ (= (get applied-tokens attr) id)) token-attributes))) -(defn tokens-applied? +(defn shapes-token-applied? "Test if `token` is applied to to any of `shapes` with the given `token-attributes`." [token shapes token-attributes] (some #(token-applied? token % token-attributes) shapes)) diff --git a/frontend/test/token_tests/token_test.cljs b/frontend/test/token_tests/token_test.cljs index 3486bed83..deea17fc0 100644 --- a/frontend/test/token_tests/token_test.cljs +++ b/frontend/test/token_tests/token_test.cljs @@ -21,14 +21,14 @@ (t/deftest tokens-applied-test (t/testing "is true when single shape matches the token and attributes" - (t/is (true? (wtt/tokens-applied? {:id :a} [{:applied-tokens {:x :a}} + (t/is (true? (wtt/shapes-token-applied? {:id :a} [{:applied-tokens {:x :a}} {:applied-tokens {:x :b}}] #{:x})))) (t/testing "is false when no shape matches the token or attributes" - (t/is (nil? (wtt/tokens-applied? {:id :a} [{:applied-tokens {:x :b}} + (t/is (nil? (wtt/shapes-token-applied? {:id :a} [{:applied-tokens {:x :b}} {:applied-tokens {:x :b}}] #{:x}))) - (t/is (nil? (wtt/tokens-applied? {:id :a} [{:applied-tokens {:x :a}} + (t/is (nil? (wtt/shapes-token-applied? {:id :a} [{:applied-tokens {:x :a}} {:applied-tokens {:x :a}}] #{:y})))))