0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-03 21:09:00 -05:00

♻️ Remove duplicate merge operation on svg_raw shapes

This commit is contained in:
Andrey Antukh 2023-09-20 19:42:47 +02:00 committed by Alonso Torres
parent 91224e5274
commit 1ab690a408

View file

@ -12,6 +12,7 @@
[app.main.ui.context :as muc] [app.main.ui.context :as muc]
[app.main.ui.shapes.attrs :as usa] [app.main.ui.shapes.attrs :as usa]
[app.util.object :as obj] [app.util.object :as obj]
[cuerdas.core :as str]
[rumext.v2 :as mf])) [rumext.v2 :as mf]))
;; Graphic tags ;; Graphic tags
@ -21,74 +22,74 @@
;; Context to store a re-mapping of the ids ;; Context to store a re-mapping of the ids
(def svg-ids-ctx (mf/create-context nil)) (def svg-ids-ctx (mf/create-context nil))
(defn set-styles [attrs shape render-id]
(let [props (-> (usa/get-style-props shape render-id)
(obj/unset! "transform"))
attrs (if (map? attrs)
(-> attrs csvg/attrs->props obj/map->obj)
#js {})
style (obj/merge (obj/get attrs "style")
(obj/get props "style"))]
(-> attrs
(obj/merge! props)
(obj/set! "style" style))))
(defn translate-shape [attrs shape]
(let [transform (dm/str (csvg/svg-transform-matrix shape)
" "
(:transform attrs ""))]
(cond-> attrs
(and (:svg-viewbox shape) (contains? graphic-element (-> shape :content :tag)))
(assoc :transform transform))))
(mf/defc svg-root (mf/defc svg-root
{::mf/wrap-props false} {::mf/wrap-props false}
[props] [props]
(let [shape (unchecked-get props "shape") (let [shape (unchecked-get props "shape")
children (unchecked-get props "children") children (unchecked-get props "children")
{:keys [x y width height]} shape
{:keys [attrs] :as content} (:content shape) x (dm/get-prop shape :x)
y (dm/get-prop shape :y)
w (dm/get-prop shape :width)
h (dm/get-prop shape :height)
ids-mapping (mf/with-memo [shape]
(csvg/generate-id-mapping (:content shape)))
ids-mapping (mf/use-memo #(csvg/generate-id-mapping content))
render-id (mf/use-ctx muc/render-id) render-id (mf/use-ctx muc/render-id)
attrs (-> (set-styles attrs shape render-id) props (mf/with-memo [shape render-id]
(obj/set! "x" x) (-> (usa/get-style-props shape render-id)
(obj/set! "y" y) (obj/unset! "transform")
(obj/set! "width" width) (obj/set! "x" x)
(obj/set! "height" height) (obj/set! "y" y)
(obj/set! "preserveAspectRatio" "none"))] (obj/set! "width" w)
(obj/set! "height" h)
(obj/set! "preserveAspectRatio" "none")))]
[:& (mf/provider svg-ids-ctx) {:value ids-mapping} [:& (mf/provider svg-ids-ctx) {:value ids-mapping}
[:g.svg-raw {:transform (gsh/transform-str shape)} [:g.svg-raw {:transform (gsh/transform-str shape)}
[:> "svg" attrs children]]])) [:> "svg" props children]]]))
(mf/defc svg-element (mf/defc svg-element
{::mf/wrap-props false} {::mf/wrap-props false}
[props] [props]
(let [shape (unchecked-get props "shape") (let [shape (unchecked-get props "shape")
children (unchecked-get props "children") children (unchecked-get props "children")
{:keys [content]} shape
{:keys [attrs tag]} content
ids-mapping (mf/use-ctx svg-ids-ctx) ids-mapping (mf/use-ctx svg-ids-ctx)
render-id (mf/use-ctx muc/render-id) render-id (mf/use-ctx muc/render-id)
attrs (mf/use-memo #(csvg/replace-attrs-ids attrs ids-mapping)) tag (-> shape :content :tag)
attrs (translate-shape attrs shape) shape
element-id (get-in content [:attrs :id]) (mf/with-memo [shape ids-mapping]
attrs (cond-> (set-styles attrs shape render-id) (let [tag (-> shape :content :tag)]
(and element-id (contains? ids-mapping element-id)) (-> shape
(obj/set! "id" (get ids-mapping element-id)))] (update :svg-attrs csvg/replace-attrs-ids ids-mapping)
[:> (name tag) attrs children])) (update :svg-attrs (fn [attrs]
(if (contains? graphic-element tag)
(assoc attrs :transform (str/ffmt "% %"
(csvg/svg-transform-matrix shape)
(:transform attrs "")))
(dissoc attrs :transform)))))))
(defn svg-raw-shape [shape-wrapper] props
(mf/with-memo [shape render-id]
(let [element-id (dm/get-in shape [:svg-attrs :id])
props (usa/get-style-props shape render-id)]
(when (and (some? element-id)
(contains? ids-mapping element-id))
(obj/set! props "id" (get ids-mapping element-id)))
props))]
[:> (name tag) props children]))
(defn svg-raw-shape
[shape-wrapper]
(mf/fnc svg-raw-shape (mf/fnc svg-raw-shape
{::mf/wrap-props false} {::mf/wrap-props false}
[props] [props]
@ -96,8 +97,8 @@
(let [shape (unchecked-get props "shape") (let [shape (unchecked-get props "shape")
childs (unchecked-get props "childs") childs (unchecked-get props "childs")
{:keys [content]} shape content (get shape :content)
{:keys [tag]} content tag (get content :tag)
svg-root? (and (map? content) (= tag :svg)) svg-root? (and (map? content) (= tag :svg))
svg-tag? (map? content) svg-tag? (map? content)
@ -105,19 +106,17 @@
valid-tag? (contains? csvg/svg-tags-list tag)] valid-tag? (contains? csvg/svg-tags-list tag)]
(cond (cond
svg-root? ^boolean svg-root?
[:& svg-root {:shape shape} [:& svg-root {:shape shape}
(for [item childs] (for [item childs]
[:& shape-wrapper {:shape item :key (dm/str (:id item))}])] [:& shape-wrapper {:shape item :key (dm/str (:id item))}])]
(and svg-tag? valid-tag?) (and ^boolean svg-tag?
^boolean valid-tag?)
[:& svg-element {:shape shape} [:& svg-element {:shape shape}
(for [item childs] (for [item childs]
[:& shape-wrapper {:shape item :key (dm/str (:id item))}])] [:& shape-wrapper {:shape item :key (dm/str (:id item))}])]
svg-leaf? ^boolean svg-leaf?
content content))))
:else nil))))