From fd2f5537cf0cb0cbf2d953dea63fd0918a435c63 Mon Sep 17 00:00:00 2001 From: Florian Schroedl Date: Mon, 22 Jul 2024 18:07:16 +0200 Subject: [PATCH] Extract common logic --- .../ui/workspace/tokens/context_menu.cljs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) 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 f66d67cb5..4ad335732 100644 --- a/frontend/src/app/main/ui/workspace/tokens/context_menu.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/context_menu.cljs @@ -210,9 +210,48 @@ :token-type-props token-type-props :selected-shapes selected-shapes}))) +(defn attribute-actions [token selected-shapes attributes] + (let [ids-by-attributes (wtt/shapes-ids-by-applied-attributes token selected-shapes attributes) + shape-ids (into #{} (map :id selected-shapes))] + {:all-selected? (wtt/shapes-applied-all? ids-by-attributes shape-ids attributes) + :shape-ids shape-ids + :selected-pred #(seq (% ids-by-attributes))})) + (defn border-radius-attribute-actions [{:keys [token-id selected-shapes] :as _props}] (let [token {:id token-id} all-attributes #{:r1 :r2 :r3 :r4} + {:keys [all-selected? selected-pred shape-ids]} (attribute-actions token selected-shapes #{:r1 :r2 :r3 :r4}) + single-attributes (->> {:r1 "Top Left" + :r2 "Top Right" + :r3 "Bottom Left" + :r4 "Bottom Right"} + (map (fn [[attr title]] + (let [selected? (selected-pred attr)] + {:title title + :selected? (and (not all-selected?) selected?) + :action #(let [props {:attributes #{attr} + :token token + :shape-ids shape-ids} + event (cond + all-selected? (-> (assoc props :attributes-to-remove #{:r1 :r2 :r3 :r4 :rx :ry}) + (wtc/apply-token)) + selected? (wtc/unapply-token props) + :else (-> (assoc props :on-update-shape wtc/update-shape-radius-single-corner) + (wtc/apply-token)))] + (st/emit! event))})))) + all-attribute (let [props {:attributes all-attributes + :token token + :shape-ids shape-ids}] + {:title "All" + :selected? all-selected? + :action #(if all-selected? + (st/emit! (wtc/unapply-token props)) + (st/emit! (wtc/apply-token (assoc props :on-update-shape wtc/update-shape-radius-all))))})] + (concat [all-attribute] single-attributes))) + +(defn spacing-attribute-actions [{:keys [token-id selected-shapes] :as _props}] + (let [token {:id token-id} + all-attributes #{:p1 :p2 :p3 :p4} ids-by-attributes (wtt/shapes-ids-by-applied-attributes token selected-shapes all-attributes) shape-ids (into #{} (map :id selected-shapes)) all-selected? (wtt/shapes-applied-all? ids-by-attributes shape-ids all-attributes)