diff --git a/backend/src/app/features/components_v2.clj b/backend/src/app/features/components_v2.clj index e5e2673f7..03240a904 100644 --- a/backend/src/app/features/components_v2.clj +++ b/backend/src/app/features/components_v2.clj @@ -198,6 +198,15 @@ (update file-data :pages-index update-vals update-page))) + ;; Sometimes we found that the file has issues in the internal + ;; data structure of the local library; this function tries to + ;; fix that issues. + fix-file-data + (fn [file-data] + (-> file-data + (d/update-when :colors dissoc nil) + (d/update-when :typographies dissoc nil))) + delete-big-geometry-shapes (fn [file-data] ;; At some point in time, we had a bug that generated shapes @@ -205,12 +214,10 @@ ;; schema. Since we don't have a way to fix those shapes, we ;; simply proceed to delete it. We ignore path type shapes ;; because they have not been affected by the bug. - (letfn [(fix-container - [container] + (letfn [(fix-container [container] (d/update-when container :objects #(reduce-kv fix-shape % %))) - (fix-shape - [objects id shape] + (fix-shape [objects id shape] (cond (or (cfh/path-shape? shape) (cfh/bool-shape? shape)) @@ -232,6 +239,33 @@ (update :pages-index update-vals fix-container) (d/update-when :components update-vals fix-container)))) + ;; Some files has totally broken shapes, we just remove them + fix-completly-broken-shapes + (fn [file-data] + (letfn [(update-object [objects id shape] + (cond + (nil? (:type shape)) + (let [ids (cfh/get-children-ids objects id)] + (-> objects + (dissoc id) + (as-> $ (reduce dissoc $ ids)) + (d/update-in-when [(:parent-id shape) :shapes] + (fn [shapes] (filterv #(not= id %) shapes))))) + + (and (cfh/text-shape? shape) + (not (seq (:content shape)))) + (dissoc objects id) + + :else + objects)) + + (update-container [container] + (d/update-when container :objects #(reduce-kv update-object % %)))] + + (-> file-data + (update :pages-index update-vals update-container) + (update :components update-vals update-container)))) + fix-misc-shape-issues (fn [file-data] (letfn [(fix-container [container] @@ -248,6 +282,9 @@ 0 gap))) + (nil? (:name shape)) + (assoc :name (d/name (:type shape))) + ;; Fix broken fills (seq (:fills shape)) (update :fills (fn [fills] (filterv valid-fill? fills))) @@ -649,7 +686,9 @@ (update :pages-index update-vals fix-container))))] (-> file-data + (fix-file-data) (fix-page-invalid-options) + (fix-completly-broken-shapes) (fix-bad-children) (fix-misc-shape-issues) (fix-recent-colors) diff --git a/common/src/app/common/files/migrations.cljc b/common/src/app/common/files/migrations.cljc index 39d448597..025f1d841 100644 --- a/common/src/app/common/files/migrations.cljc +++ b/common/src/app/common/files/migrations.cljc @@ -697,9 +697,16 @@ (defmethod migrate 39 [data] (letfn [(update-shape [shape] - (if (and (cfh/bool-shape? shape) - (not (contains? shape :bool-content))) + (cond + (and (cfh/bool-shape? shape) + (not (contains? shape :bool-content))) (assoc shape :bool-content []) + + (and (cfh/path-shape? shape) + (not (contains? shape :content))) + (assoc shape :content []) + + :else shape)) (update-container [container] @@ -788,9 +795,16 @@ (defmethod migrate 43 [data] - (letfn [(update-text-node [node] + (letfn [(number->string [v] + (if (number? v) + (str v) + v)) + + (update-text-node [node] (-> node (d/update-when :fills #(filterv valid-fill? %)) + (d/update-when :font-size number->string) + (d/update-when :font-weight number->string) (d/without-nils))) (update-object [object] diff --git a/common/src/app/common/svg.cljc b/common/src/app/common/svg.cljc index e0c718362..4ea6278cc 100644 --- a/common/src/app/common/svg.cljc +++ b/common/src/app/common/svg.cljc @@ -800,15 +800,24 @@ "skewX" (apply gmt/skew-matrix (format-skew-x-params params)) "skewY" (apply gmt/skew-matrix (format-skew-y-params params)))) +(def ^:private + xf-parse-numbers + (comp + (map first) + (keep not-empty) + (map d/parse-double))) + +(defn parse-numbers + [data] + (->> (re-seq number-regex data) + (into [] xf-parse-numbers))) + (defn parse-transform [transform] (if (string? transform) (->> (re-seq matrices-regex transform) (map (fn [[_ type params]] - (let [params (->> (re-seq number-regex params) - (map first) - (keep not-empty) - (map d/parse-double))] + (let [params (parse-numbers params)] (to-matrix type params)))) (reduce gmt/multiply (gmt/matrix))) diff --git a/common/src/app/common/svg/shapes_builder.cljc b/common/src/app/common/svg/shapes_builder.cljc index 343507f77..a831d3c0e 100644 --- a/common/src/app/common/svg/shapes_builder.cljc +++ b/common/src/app/common/svg/shapes_builder.cljc @@ -63,8 +63,7 @@ viewbox (or (:viewBox attrs) (dm/str "0 0 " width " " height)) - [x y width height] (->> (str/split viewbox #"[\s,]+") - (map d/parse-double)) + [x y width height] (csvg/parse-numbers viewbox) width (if (= width 0) 1 width) height (if (= height 0) 1 height)]