0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 16:00:19 -05:00

🐛 Fix style scoping problem with imported SVG

This commit is contained in:
alonso.torres 2024-05-03 15:00:51 +02:00
parent 23bd57e9bb
commit 7117ea1f7e
4 changed files with 34 additions and 10 deletions

View file

@ -10,6 +10,7 @@
### :bug: Bugs fixed ### :bug: Bugs fixed
- Fix color palette sorting [Taiga Issue #7458](https://tree.taiga.io/project/penpot/issue/7458) - Fix color palette sorting [Taiga Issue #7458](https://tree.taiga.io/project/penpot/issue/7458)
- Fix style scoping problem with imported SVG [Taiga #7671](https://tree.taiga.io/project/penpot/issue/7671)
## 2.0.1 ## 2.0.1

View file

@ -17,6 +17,7 @@
(def current-page-id (mf/create-context nil)) (def current-page-id (mf/create-context nil))
(def current-file-id (mf/create-context nil)) (def current-file-id (mf/create-context nil))
(def current-vbox (mf/create-context nil)) (def current-vbox (mf/create-context nil))
(def current-svg-root-id (mf/create-context nil))
(def active-frames (mf/create-context nil)) (def active-frames (mf/create-context nil))
(def render-thumbnails (mf/create-context nil)) (def render-thumbnails (mf/create-context nil))

View file

@ -17,6 +17,7 @@
(mf/fnc group-shape (mf/fnc group-shape
{::mf/wrap-props false} {::mf/wrap-props false}
[props] [props]
(let [shape (unchecked-get props "shape") (let [shape (unchecked-get props "shape")
childs (unchecked-get props "childs") childs (unchecked-get props "childs")
render-id (mf/use-ctx muc/render-id) render-id (mf/use-ctx muc/render-id)
@ -36,21 +37,31 @@
mask-props (if ^boolean masked-group? mask-props (if ^boolean masked-group?
#js {:mask (mask-url render-id mask)} #js {:mask (mask-url render-id mask)}
#js {})] #js {})
current-svg-root-id (mf/use-ctx muc/current-svg-root-id)
;; We need to create a "scope" for svg classes. The root of the imported SVG (first group) will
;; be stored in the context. When rendering the styles we add its id as prefix.
[svg-wrapper svg-wrapper-props]
(if (and (contains? shape :svg-attrs) (not current-svg-root-id))
[(mf/provider muc/current-svg-root-id) #js {:value (:id shape)}]
[mf/Fragment #js {}])]
;; We need to separate mask and clip into two because a bug in ;; We need to separate mask and clip into two because a bug in
;; Firefox breaks when the group has clip+mask+foreignObject ;; Firefox breaks when the group has clip+mask+foreignObject
;; Clip and mask separated will work in every platform Firefox ;; Clip and mask separated will work in every platform Firefox
;; bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1734805 ;; bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1734805
[:> wrapper clip-props [:> svg-wrapper svg-wrapper-props
[:> wrapper mask-props [:> wrapper clip-props
(when ^boolean masked-group? [:> wrapper mask-props
[:& render-mask {:mask mask}]) (when ^boolean masked-group?
[:& render-mask {:mask mask}])
(for [item childs] (for [item childs]
[:& shape-wrapper [:& shape-wrapper
{:shape item {:shape item
:key (dm/str (dm/get-prop item :id))}])]])))) :key (dm/str (dm/get-prop item :id))}])]]]))))

View file

@ -104,9 +104,20 @@
svg-root? (and (map? content) (= tag :svg)) svg-root? (and (map? content) (= tag :svg))
svg-tag? (map? content) svg-tag? (map? content)
svg-leaf? (string? content) svg-leaf? (string? content)
valid-tag? (contains? csvg/svg-tags tag)] valid-tag? (contains? csvg/svg-tags tag)
current-svg-root-id (mf/use-ctx muc/current-svg-root-id)
;; We need to create a "scope" for svg classes. The root of the imported SVG (first group) will
;; be stored in the context and with this we scoped the styles:
style-content
(when (= tag :style)
(dm/str "#shape-" current-svg-root-id "{ " (->> shape :content :content (str/join "\n")) " }"))]
(cond (cond
(= tag :style)
[:style style-content]
^boolean svg-root? ^boolean svg-root?
[:& svg-root {:shape shape} [:& svg-root {:shape shape}
(for [item childs] (for [item childs]