Add minor optimization to type->options resolution on measures-menu

This commit is contained in:
Andrey Antukh 2025-01-16 14:35:53 +01:00
parent 0b18177925
commit ef7fbc09b0

View file

@ -33,7 +33,7 @@
[app.main.ui.workspace.tokens.token-types :as wtty] [app.main.ui.workspace.tokens.token-types :as wtty]
[app.util.dom :as dom] [app.util.dom :as dom]
[app.util.i18n :as i18n :refer [tr]] [app.util.i18n :as i18n :refer [tr]]
[clojure.set :refer [rename-keys union]] [clojure.set :as set]
[rumext.v2 :as mf])) [rumext.v2 :as mf]))
(def measure-attrs (def measure-attrs
@ -48,16 +48,27 @@
:show-content :show-content
:hide-in-viewer]) :hide-in-viewer])
(def ^:private type->options (def ^:private generic-options
{:bool #{:size :position :rotation} #{:size :position :rotation})
:circle #{:size :position :rotation}
:frame #{:presets :size :position :rotation :radius :clip-content :show-in-viewer} (def ^:private rect-options
:group #{:size :position :rotation} #{:size :position :rotation :radius})
:image #{:size :position :rotation :radius}
:path #{:size :position :rotation} (def ^:private frame-options
:rect #{:size :position :rotation :radius} #{:presets :size :position :rotation :radius :clip-content :show-in-viewer})
:svg-raw #{:size :position :rotation}
:text #{:size :position :rotation}}) (defn- type->options
[type]
(case type
:bool generic-options
:circle generic-options
:frame frame-options
:group generic-options
:image rect-options
:path generic-options
:rect rect-options
:svg-raw generic-options
:text generic-options))
(def ^:private clip-content-icon (i/icon-xref :clip-content (stl/css :checkbox-button))) (def ^:private clip-content-icon (i/icon-xref :clip-content (stl/css :checkbox-button)))
(def ^:private play-icon (i/icon-xref :play (stl/css :checkbox-button))) (def ^:private play-icon (i/icon-xref :play (stl/css :checkbox-button)))
@ -67,37 +78,45 @@
(defn select-measure-keys (defn select-measure-keys
"Consider some shapes can be drawn from bottom to top or from left to right" "Consider some shapes can be drawn from bottom to top or from left to right"
[shape] [shape]
(let [shape (cond (let [flip-x (get shape :flip-x)
(and (:flip-x shape) (:flip-y shape)) flip-y (get shape :flip-y)
(rename-keys shape {:r1 :r3 :r2 :r4 :r3 :r1 :r4 :r2})
(:flip-x shape) shape (cond
(rename-keys shape {:r1 :r2 :r2 :r1 :r3 :r4 :r4 :r3}) (and flip-x flip-y)
(set/rename-keys shape {:r1 :r3 :r2 :r4 :r3 :r1 :r4 :r2})
(:flip-y shape) flip-x
(rename-keys shape {:r1 :r4 :r2 :r3 :r3 :r2 :r4 :r1}) (set/rename-keys shape {:r1 :r2 :r2 :r1 :r3 :r4 :r4 :r3})
flip-y
(set/rename-keys shape {:r1 :r4 :r2 :r3 :r3 :r2 :r4 :r1})
:else :else
shape)] shape)]
(select-keys shape measure-attrs))) (select-keys shape measure-attrs)))
;; -- User/drawing coords
(mf/defc measures-menu (mf/defc measures-menu
{::mf/wrap-props false {::mf/props :obj
::mf/wrap [mf/memo]} ::mf/wrap [mf/memo]}
[{:keys [ids ids-with-children values type all-types shape]}] [{:keys [ids ids-with-children values type all-types shape]}]
(let [options (if (= type :multiple) (let [design-tokens? (mf/use-ctx muc/design-tokens)
(reduce #(union %1 %2) (map #(get type->options %) all-types))
(get type->options type))
design-tokens? (mf/use-ctx muc/design-tokens) options
(mf/with-memo [type all-types]
(if (= type :multiple)
(into #{} (mapcat type->options) all-types)
(type->options type)))
ids-with-children (or ids-with-children ids) ids-with-children
(or ids-with-children ids)
old-shapes (if (= type :multiple) old-shapes
(if (= type :multiple)
(deref (refs/objects-by-id ids)) (deref (refs/objects-by-id ids))
[shape]) [shape])
frames (map #(deref (refs/object-by-id (:frame-id %))) old-shapes)
frames
(map #(deref (refs/object-by-id (:frame-id %))) old-shapes)
ids (hooks/use-equal-memo ids) ids (hooks/use-equal-memo ids)