Merge pull request #966 from penpot/remove-back-xml-parse

⬆️ Move svg parsing to the frontend with Tubax
This commit is contained in:
Andrey Antukh 2021-05-20 11:57:59 +02:00 committed by GitHub
commit 1ff9b24818
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 32 additions and 20 deletions

View File

@ -48,6 +48,7 @@
"react-dom": "~17.0.1", "react-dom": "~17.0.1",
"react-virtualized": "^9.22.3", "react-virtualized": "^9.22.3",
"rxjs": "~7.0.1", "rxjs": "~7.0.1",
"sax": "^1.2.4",
"source-map-support": "^0.5.16", "source-map-support": "^0.5.16",
"tdigest": "^0.1.1", "tdigest": "^0.1.1",
"ua-parser-js": "^0.7.28", "ua-parser-js": "^0.7.28",

View File

@ -20,6 +20,7 @@
[funcool/potok "4.0.0"] [funcool/potok "4.0.0"]
[funcool/promesa "6.0.0"] [funcool/promesa "6.0.0"]
[funcool/rumext "2021.05.12-1"] [funcool/rumext "2021.05.12-1"]
[funcool/tubax "2021.05.20-0"]
[lambdaisland/uri "1.4.54" [lambdaisland/uri "1.4.54"
:exclusions [org.clojure/data.json]] :exclusions [org.clojure/data.json]]

View File

@ -14,8 +14,8 @@
[app.common.spec :as us] [app.common.spec :as us]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.main.data.dashboard :as dd] [app.main.data.dashboard :as dd]
[app.main.data.media :as di]
[app.main.data.fonts :as df] [app.main.data.fonts :as df]
[app.main.data.media :as di]
[app.main.data.messages :as dm] [app.main.data.messages :as dm]
[app.main.data.workspace.changes :as dch] [app.main.data.workspace.changes :as dch]
[app.main.data.workspace.common :as dwc] [app.main.data.workspace.common :as dwc]
@ -39,7 +39,8 @@
[clojure.set :as set] [clojure.set :as set]
[cuerdas.core :as str] [cuerdas.core :as str]
[potok.core :as ptk] [potok.core :as ptk]
[promesa.core :as p])) [promesa.core :as p]
[tubax.core :as tubax]))
(declare persist-changes) (declare persist-changes)
(declare persist-sychronous-changes) (declare persist-sychronous-changes)
@ -344,8 +345,12 @@
(defn parse-svg (defn parse-svg
[[name text]] [[name text]]
(->> (rp/query! :parsed-svg {:data text}) (try
(rx/map #(assoc % :name name)))) (->> (rx/of (-> (tubax/xml->clj text)
(assoc :name name))))
(catch :default err
(rx/throw {:type :svg-parser}))))
(defn fetch-svg [name uri] (defn fetch-svg [name uri]
(->> (http/send! {:method :get :uri uri :mode :no-cors}) (->> (http/send! {:method :get :uri uri :mode :no-cors})

View File

@ -110,14 +110,6 @@
:response-type :blob}) :response-type :blob})
(rx/mapcat handle-response))) (rx/mapcat handle-response)))
(defmethod query :parsed-svg
[id params]
(->> (http/send! {:method :post
:uri (u/join base-uri "api/rpc/query/" (name id))
:body (http/transit-data params)})
(rx/map http/conditional-decode-transit)
(rx/mapcat handle-response)))
(derive :upload-file-media-object ::multipart-upload) (derive :upload-file-media-object ::multipart-upload)
(derive :update-profile-photo ::multipart-upload) (derive :update-profile-photo ::multipart-upload)
(derive :update-team-photo ::multipart-upload) (derive :update-team-photo ::multipart-upload)

View File

@ -240,6 +240,15 @@
(js/console.error explain))) (js/console.error explain)))
(js/console.groupEnd "Validation Error")) (js/console.groupEnd "Validation Error"))
;; Error on parsing an SVG
(defmethod ptk/handle-error :svg-parser
[error]
(ts/schedule
(st/emitf
(dm/show {:content "SVG is invalid or malformed"
:type :error
:timeout 3000}))))
;; This is a pure frontend error that can be caused by an active ;; This is a pure frontend error that can be caused by an active
;; assertion (assertion that is preserved on production builds). From ;; assertion (assertion that is preserved on production builds). From
;; the user perspective this should be treated as internal error. ;; the user perspective this should be treated as internal error.

View File

@ -23,7 +23,7 @@
(defn index-shape (defn index-shape
[objects parents-index masks-index] [objects parents-index masks-index]
(fn [index shape] (fn [index shape]
(let [{:keys [x y width height]} (:selrect shape) (let [{:keys [x y width height]} (gsh/points->selrect (:points shape))
shape-bound #js {:x x :y y :width width :height height} shape-bound #js {:x x :y y :width width :height height}
parents (get parents-index (:id shape)) parents (get parents-index (:id shape))
@ -42,11 +42,10 @@
(let [shapes (-> objects (dissoc uuid/zero) (vals)) (let [shapes (-> objects (dissoc uuid/zero) (vals))
parents-index (cp/generate-child-all-parents-index objects) parents-index (cp/generate-child-all-parents-index objects)
masks-index (cp/create-mask-index objects parents-index) masks-index (cp/create-mask-index objects parents-index)
bounds (gsh/selection-rect shapes) bounds #js {:x (int -0.5e7)
bounds #js {:x (:x bounds) :y (int -0.5e7)
:y (:y bounds) :width (int 1e7)
:width (:width bounds) :height (int 1e7)}
:height (:height bounds)}
index (reduce (index-shape objects parents-index masks-index) index (reduce (index-shape objects parents-index masks-index)
(qdt/create bounds) (qdt/create bounds)
@ -65,7 +64,8 @@
(get new-objects id))) (get new-objects id)))
changed-ids (into #{} changed-ids (into #{}
(filter changes?) (comp (filter changes?)
(filter #(not= % uuid/zero)))
(set/union (keys old-objects) (set/union (keys old-objects)
(keys new-objects))) (keys new-objects)))

4
frontend/vendor/tubax/saxjs.cljs vendored Normal file
View File

@ -0,0 +1,4 @@
(ns tubax.saxjs
(:require ["sax" :as sax]))
(goog/exportSymbol "sax" sax)

View File

@ -4478,7 +4478,7 @@ sass@^1.32.8:
dependencies: dependencies:
chokidar ">=3.0.0 <4.0.0" chokidar ">=3.0.0 <4.0.0"
sax@~1.2.4: sax@^1.2.4, sax@~1.2.4:
version "1.2.4" version "1.2.4"
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw== integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==