Repository: penpot/penpot
File: frontend/src/app/main/data/workspace/tokens/application.cljs (ClojureScript)

Code before:
   [app.common.files.tokens :as cft]
   [app.common.types.shape.layout :as ctsl]
   [app.common.types.shape.radius :as ctsr]
   [app.common.types.text :as txt]
   [app.common.types.token :as ctt]
   [app.common.types.tokens-lib :as ctob]
   [app.common.types.typography :as cty]
   [app.common.uuid :as uuid]
   [app.main.data.event :as ev]
   [app.main.data.helpers :as dsh]
   [app.main.data.style-dictionary :as sd]
   [app.main.data.tinycolor :as tinycolor]
   [app.main.data.workspace :as udw]
   [app.main.data.workspace.colors :as wdc]
   [app.main.data.workspace.shape-layout :as dwsl]
   [app.main.data.workspace.shapes :as dwsh]
   [app.main.data.workspace.transforms :as dwtr]
   [app.main.data.workspace.undo :as dwu]
   [app.main.fonts :as fonts]
   [app.main.store :as st]
   [beicon.v2.core :as rx]
   [clojure.set :as set]
   [potok.v2.core :as ptk]))

(declare token-properties)

;; Events to apply / unapply tokens to shapes ------------------------------------------------------------

(defn apply-token
  "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]}]
  (ptk/reify ::apply-token
    ptk/WatchEvent
    (watch [_ state _]
      ;; We do not allow to apply tokens while text editor is open.
      (when (empty? (get state :workspace-editor-state))
        (when-let [tokens (some-> (dsh/lookup-file-data state)
                                  (get :tokens-lib)
                                  (ctob/get-tokens-in-active-sets))]
          (->> (sd/resolve-tokens tokens)
               (rx/mapcat
                (fn [resolved-tokens]
                  (let [undo-id (js/Symbol)
                        objects (dsh/lookup-page-objects state)

                        shape-ids (or (->> (select-keys objects shape-ids)
                                           (filter (fn [[_ shape]]

Review comment (suggestion):
This file had to be rearranged so we can re-use `update-layout-item-margin` directly in the `apply-spacing-token` function

Code after:
   [app.common.files.tokens :as cft]
   [app.common.types.shape.layout :as ctsl]
   [app.common.types.shape.radius :as ctsr]
   [app.common.types.text :as txt]
   [app.common.types.token :as ctt]
   [app.common.types.tokens-lib :as ctob]
   [app.common.types.typography :as cty]
   [app.common.uuid :as uuid]
   [app.main.data.event :as ev]
   [app.main.data.helpers :as dsh]
   [app.main.data.style-dictionary :as sd]
   [app.main.data.tinycolor :as tinycolor]
   [app.main.data.workspace :as udw]
   [app.main.data.workspace.colors :as wdc]
   [app.main.data.workspace.shape-layout :as dwsl]
   [app.main.data.workspace.shapes :as dwsh]
   [app.main.data.workspace.transforms :as dwtr]
   [app.main.data.workspace.undo :as dwu]
   [app.main.fonts :as fonts]
   [app.main.store :as st]
   [beicon.v2.core :as rx]
   [clojure.set :as set]
   [potok.v2.core :as ptk]))

(declare token-properties)

;; Events to update the value of attributes with applied tokens ---------------------------------------------------------

;; (note that dwsh/update-shapes function returns an event)

(defn update-shape-radius-all
  ([value shape-ids attributes] (update-shape-radius-all value shape-ids attributes nil))
  ([value shape-ids _attributes page-id] ; The attributes param is needed to have the same arity that other update functions
   (when (number? value)
     (dwsh/update-shapes shape-ids
                         (fn [shape]
                           (ctsr/set-radius-to-all-corners shape value))
                         {:reg-objects? true
                          :ignore-touched true
                          :page-id page-id
                          :attrs ctt/border-radius-keys}))))

(defn update-shape-radius-for-corners
  ([value shape-ids attributes] (update-shape-radius-for-corners value shape-ids attributes nil))
  ([value shape-ids attributes page-id]
   (when (number? value)
     (dwsh/update-shapes shape-ids
                         (fn [shape]
                           (ctsr/set-radius-for-corners shape attributes value))
                         {:reg-objects? true
                          :ignore-touched true