0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 07:50:43 -05:00

🐛 Fix error streen when uploading wrong SVG

This commit is contained in:
alonso.torres 2023-03-02 16:24:23 +01:00
parent 7b72906096
commit 6962e15b6d
4 changed files with 103 additions and 93 deletions

View file

@ -16,6 +16,7 @@
### :bug: Bugs fixed ### :bug: Bugs fixed
- Fix problem with rules position on changing pages [Taiga #4847](https://tree.taiga.io/project/penpot/issue/4847) - Fix problem with rules position on changing pages [Taiga #4847](https://tree.taiga.io/project/penpot/issue/4847)
- Fix error streen when uploading wrong SVG [#2995](https://github.com/penpot/penpot/issues/2995)
### :arrow_up: Deps updates ### :arrow_up: Deps updates

View file

@ -62,7 +62,9 @@
height (get-in data [:attrs :height] 100) height (get-in data [:attrs :height] 100)
viewbox (get-in data [:attrs :viewBox] (str "0 0 " width " " height)) viewbox (get-in data [:attrs :viewBox] (str "0 0 " width " " height))
[x y width height] (->> (str/split viewbox #"\s+") [x y width height] (->> (str/split viewbox #"\s+")
(map d/parse-double))] (map d/parse-double))
width (if (= width 0) 1 width)
height (if (= height 0) 1 height)]
[(assert-valid-num :x x) [(assert-valid-num :x x)
(assert-valid-num :y y) (assert-valid-num :y y)
(assert-valid-pos-num :width width) (assert-valid-pos-num :width width)
@ -483,117 +485,118 @@
(defn create-svg-shapes (defn create-svg-shapes
[svg-data {:keys [x y]} objects frame-id parent-id selected center?] [svg-data {:keys [x y]} objects frame-id parent-id selected center?]
(try (let [[vb-x vb-y vb-width vb-height] (svg-dimensions svg-data)
(let [[vb-x vb-y vb-width vb-height] (svg-dimensions svg-data) x (mth/round
x (mth/round (if center?
(if center? (- x vb-x (/ vb-width 2))
(- x vb-x (/ vb-width 2)) x))
x)) y (mth/round
y (mth/round (if center?
(if center? (- y vb-y (/ vb-height 2))
(- y vb-y (/ vb-height 2)) y))
y))
unames (cp/retrieve-used-names objects) unames (cp/retrieve-used-names objects)
svg-name (str/replace (:name svg-data) ".svg" "") svg-name (str/replace (:name svg-data) ".svg" "")
svg-data (-> svg-data svg-data (-> svg-data
(assoc :x x (assoc :x x
:y y :y y
:offset-x vb-x :offset-x vb-x
:offset-y vb-y :offset-y vb-y
:width vb-width :width vb-width
:height vb-height :height vb-height
:name svg-name)) :name svg-name))
[def-nodes svg-data] (-> svg-data [def-nodes svg-data] (-> svg-data
(usvg/fix-default-values) (usvg/fix-default-values)
(usvg/fix-percents) (usvg/fix-percents)
(usvg/extract-defs)) (usvg/extract-defs))
svg-data (assoc svg-data :defs def-nodes) svg-data (assoc svg-data :defs def-nodes)
root-shape (create-svg-root frame-id parent-id svg-data) root-shape (create-svg-root frame-id parent-id svg-data)
root-id (:id root-shape) root-id (:id root-shape)
;; In penpot groups have the size of their children. To respect the imported svg size and empty space let's create a transparent shape as background to respect the imported size ;; In penpot groups have the size of their children. To respect the imported svg size and empty space let's create a transparent shape as background to respect the imported size
base-background-shape {:tag :rect base-background-shape {:tag :rect
:attrs {:x (str vb-x) :attrs {:x (str vb-x)
:y (str vb-y) :y (str vb-y)
:width (str vb-width) :width (str vb-width)
:height (str vb-height) :height (str vb-height)
:fill "none" :fill "none"
:id "base-background"} :id "base-background"}
:hidden true :hidden true
:content []} :content []}
svg-data (-> svg-data svg-data (-> svg-data
(assoc :defs def-nodes) (assoc :defs def-nodes)
(assoc :content (into [base-background-shape] (:content svg-data)))) (assoc :content (into [base-background-shape] (:content svg-data))))
;; Create the root shape ;; Create the root shape
new-shape (dwsh/make-new-shape root-shape objects selected) new-shape (dwsh/make-new-shape root-shape objects selected)
root-attrs (-> (:attrs svg-data) root-attrs (-> (:attrs svg-data)
(usvg/format-styles)) (usvg/format-styles))
[_ new-children] [_ new-children]
(reduce (partial create-svg-children objects selected frame-id root-id svg-data) (reduce (partial create-svg-children objects selected frame-id root-id svg-data)
[unames []] [unames []]
(d/enumerate (->> (:content svg-data) (d/enumerate (->> (:content svg-data)
(mapv #(usvg/inherit-attributes root-attrs %)))))] (mapv #(usvg/inherit-attributes root-attrs %)))))]
[new-shape new-children]) [new-shape new-children]))
(catch :default e
(.error js/console "Error SVG" e)
(rx/throw {:type :svg-parser
:data e}))))
(defn add-svg-shapes (defn add-svg-shapes
[svg-data position] [svg-data position]
(ptk/reify ::add-svg-shapes (ptk/reify ::add-svg-shapes
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(let [page-id (:current-page-id state) (try
objects (wsh/lookup-page-objects state page-id) (let [page-id (:current-page-id state)
frame-id (ctst/top-nested-frame objects position) objects (wsh/lookup-page-objects state page-id)
selected (wsh/lookup-selected state) frame-id (ctst/top-nested-frame objects position)
page-objects (wsh/lookup-page-objects state) selected (wsh/lookup-selected state)
base (cph/get-base-shape page-objects selected) page-objects (wsh/lookup-page-objects state)
selected-frame? (and (= 1 (count selected)) base (cph/get-base-shape page-objects selected)
(= :frame (get-in objects [(first selected) :type]))) selected-frame? (and (= 1 (count selected))
(= :frame (get-in objects [(first selected) :type])))
parent-id (if parent-id
(or selected-frame? (empty? selected)) frame-id (if (or selected-frame? (empty? selected))
(:parent-id base)) frame-id
(:parent-id base))
[new-shape new-children] [new-shape new-children]
(create-svg-shapes svg-data position objects frame-id parent-id selected true) (create-svg-shapes svg-data position objects frame-id parent-id selected true)
changes (-> (pcb/empty-changes it page-id) changes (-> (pcb/empty-changes it page-id)
(pcb/with-objects objects) (pcb/with-objects objects)
(pcb/add-object new-shape)) (pcb/add-object new-shape))
changes changes
(reduce (fn [changes [index new-child]] (reduce (fn [changes [index new-child]]
(-> changes (-> changes
(pcb/add-object new-child) (pcb/add-object new-child)
(pcb/change-parent (:parent-id new-child) [new-child] index))) (pcb/change-parent (:parent-id new-child) [new-child] index)))
changes changes
(d/enumerate new-children)) (d/enumerate new-children))
changes (pcb/resize-parents changes changes (pcb/resize-parents changes
(->> changes (->> changes
:redo-changes :redo-changes
(filter #(= :add-obj (:type %))) (filter #(= :add-obj (:type %)))
(map :id) (map :id)
reverse reverse
vec)) vec))
undo-id (js/Symbol)] undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id) (rx/of (dwu/start-undo-transaction undo-id)
(dch/commit-changes changes) (dch/commit-changes changes)
(dws/select-shapes (d/ordered-set (:id new-shape))) (dws/select-shapes (d/ordered-set (:id new-shape)))
(ptk/data-event :layout/update [(:id new-shape)]) (ptk/data-event :layout/update [(:id new-shape)])
(dwu/commit-undo-transaction undo-id)))))) (dwu/commit-undo-transaction undo-id)))
(catch :default e
(.error js/console "Error SVG" e)
(rx/throw {:type :svg-parser
:data e}))))))

View file

@ -46,7 +46,9 @@
(defonce state (defonce state
(ptk/store {:resolve ptk/resolve (ptk/store {:resolve ptk/resolve
:on-event on-event :on-event on-event
:on-error (fn [e] (@on-error e))})) :on-error (fn [e]
(.log js/console "ERROR!!" e)
(@on-error e))}))
(defonce stream (defonce stream
(ptk/input-stream state)) (ptk/input-stream state))

View file

@ -820,6 +820,10 @@
(defn line->path [{:keys [attrs] :as node}] (defn line->path [{:keys [attrs] :as node}]
(let [tag :path (let [tag :path
{:keys [x1 y1 x2 y2]} attrs {:keys [x1 y1 x2 y2]} attrs
x1 (or x1 0)
y1 (or y1 0)
x2 (or x2 0)
y2 (or y2 0)
attrs (-> attrs attrs (-> attrs
(dissoc :x1 :x2 :y1 :y2) (dissoc :x1 :x2 :y1 :y2)
(assoc :d (str "M" x1 "," y1 " L" x2 "," y2)))] (assoc :d (str "M" x1 "," y1 " L" x2 "," y2)))]