mirror of
https://github.com/penpot/penpot.git
synced 2025-07-27 06:57:18 +02:00
Use toggle function
This commit is contained in:
parent
5cef23267c
commit
f2358b9827
5 changed files with 50 additions and 11 deletions
|
@ -213,7 +213,7 @@
|
||||||
(defn additional-actions [{:keys [token-id token-type selected-shapes] :as context-data}]
|
(defn additional-actions [{:keys [token-id token-type selected-shapes] :as context-data}]
|
||||||
(let [attributes->actions (fn [update-fn coll]
|
(let [attributes->actions (fn [update-fn coll]
|
||||||
(for [{:keys [attributes] :as item} 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
|
(assoc item
|
||||||
:action #(update-fn context-data attributes)
|
:action #(update-fn context-data attributes)
|
||||||
:selected? selected?))))]
|
:selected? selected?))))]
|
||||||
|
|
|
@ -21,7 +21,8 @@
|
||||||
[app.util.dom :as dom]
|
[app.util.dom :as dom]
|
||||||
[app.util.webapi :as wapi]
|
[app.util.webapi :as wapi]
|
||||||
[cuerdas.core :as str]
|
[cuerdas.core :as str]
|
||||||
[promesa.core :as p]))
|
[promesa.core :as p]
|
||||||
|
[clojure.set :as set]))
|
||||||
|
|
||||||
;; Helpers ---------------------------------------------------------------------
|
;; Helpers ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -48,13 +49,51 @@
|
||||||
|
|
||||||
;; Update functions ------------------------------------------------------------
|
;; 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}]
|
(defn on-apply-token [{:keys [token token-type-props selected-shapes] :as _props}]
|
||||||
(let [{:keys [attributes on-apply on-update-shape]
|
(let [{:keys [attributes on-apply on-update-shape]
|
||||||
:or {on-apply dt/update-token-from-attributes}} token-type-props
|
:or {on-apply dt/update-token-from-attributes}} token-type-props
|
||||||
shape-ids (->> selected-shapes
|
shape-ids (->> selected-shapes
|
||||||
(eduction
|
(eduction
|
||||||
(remove #(wtt/tokens-applied? token % attributes))
|
(remove #(wtt/shapes-token-applied? token % attributes))
|
||||||
(map :id)))]
|
(map :id)))]
|
||||||
|
|
||||||
(p/let [sd-tokens (sd/resolve-workspace-tokens+ {:debug? true})]
|
(p/let [sd-tokens (sd/resolve-workspace-tokens+ {:debug? true})]
|
||||||
(let [resolved-token (get sd-tokens (:id token))
|
(let [resolved-token (get sd-tokens (:id token))
|
||||||
resolved-token-value (resolve-token-value resolved-token)]
|
resolved-token-value (resolve-token-value resolved-token)]
|
||||||
|
|
|
@ -99,9 +99,9 @@
|
||||||
(mf/deps selected-shapes token-type-props)
|
(mf/deps selected-shapes token-type-props)
|
||||||
(fn [event token]
|
(fn [event token]
|
||||||
(dom/stop-propagation event)
|
(dom/stop-propagation event)
|
||||||
(wtc/on-apply-token {:token token
|
(wtc/on-toggle-token {:token token
|
||||||
:token-type-props token-type-props
|
:shapes selected-shapes
|
||||||
:selected-shapes selected-shapes})))
|
:token-type-props token-type-props})))
|
||||||
tokens-count (count tokens)]
|
tokens-count (count tokens)]
|
||||||
[:div {:on-click on-toggle-open-click}
|
[:div {:on-click on-toggle-open-click}
|
||||||
[:& cmm/asset-section {:icon (mf/fnc icon-wrapper [_]
|
[:& cmm/asset-section {:icon (mf/fnc icon-wrapper [_]
|
||||||
|
@ -122,7 +122,7 @@
|
||||||
[:& token-pill
|
[:& token-pill
|
||||||
{:key (:id token)
|
{:key (:id token)
|
||||||
:token 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-click #(on-token-pill-click % token)
|
||||||
:on-context-menu #(on-context-menu % token)}])]])]]))
|
:on-context-menu #(on-context-menu % token)}])]])]]))
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
(= (get applied-tokens attr) id))
|
(= (get applied-tokens attr) id))
|
||||||
token-attributes)))
|
token-attributes)))
|
||||||
|
|
||||||
(defn tokens-applied?
|
(defn shapes-token-applied?
|
||||||
"Test if `token` is applied to to any of `shapes` with the given `token-attributes`."
|
"Test if `token` is applied to to any of `shapes` with the given `token-attributes`."
|
||||||
[token shapes token-attributes]
|
[token shapes token-attributes]
|
||||||
(some #(token-applied? token % token-attributes) shapes))
|
(some #(token-applied? token % token-attributes) shapes))
|
||||||
|
|
|
@ -21,14 +21,14 @@
|
||||||
|
|
||||||
(t/deftest tokens-applied-test
|
(t/deftest tokens-applied-test
|
||||||
(t/testing "is true when single shape matches the token and attributes"
|
(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}}]
|
{:applied-tokens {:x :b}}]
|
||||||
#{:x}))))
|
#{:x}))))
|
||||||
(t/testing "is false when no shape matches the token or attributes"
|
(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}}]
|
{:applied-tokens {:x :b}}]
|
||||||
#{:x})))
|
#{: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}}]
|
{:applied-tokens {:x :a}}]
|
||||||
#{:y})))))
|
#{:y})))))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue