💄 Unify source code style of repair functions

This commit is contained in:
Andrés Moya 2024-01-23 14:30:06 +01:00 committed by Andrey Antukh
parent c679b04ad5
commit 02cb75209c

View file

@ -138,101 +138,101 @@
fix-missing-image-metadata fix-missing-image-metadata
(fn [file-data] (fn [file-data]
(let [update-object ;; Delete broken image shapes with no metadata.
(fn [objects id shape] (letfn [(fix-container
(if (and (cfh/image-shape? shape) [container]
(nil? (:metadata shape))) (d/update-when container :objects #(reduce-kv fix-shape % %)))
(-> objects
(dissoc id)
(d/update-in-when [(:parent-id shape) :shapes]
(fn [shapes] (filterv #(not= id %) shapes))))
objects))
update-page (fix-shape
(fn [page] [objects id shape]
(d/update-when page :objects #(reduce-kv update-object % %)))] (if (and (cfh/image-shape? shape)
(nil? (:metadata shape)))
(-> objects
(dissoc id)
(d/update-in-when [(:parent-id shape) :shapes]
(fn [shapes] (filterv #(not= id %) shapes))))
objects))]
(-> file-data (-> file-data
(update :pages-index update-vals update-page) (update :pages-index update-vals fix-container)
(d/update-when :components update-vals update-page)))) (d/update-when :components update-vals fix-container))))
;; At some point in time, we had a bug that generated shapes delete-big-geometry-shapes
;; with huge geometries that did not validate the
;; 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.
fix-big-invalid-shapes
(fn [file-data] (fn [file-data]
(let [update-object ;; At some point in time, we had a bug that generated shapes
(fn [objects id shape] ;; with huge geometries that did not validate the
(cond ;; schema. Since we don't have a way to fix those shapes, we
(or (cfh/path-shape? shape) ;; simply proceed to delete it. We ignore path type shapes
(cfh/bool-shape? shape)) ;; because they have not been affected by the bug.
objects (letfn [(fix-container
[container]
(d/update-when container :objects #(reduce-kv fix-shape % %)))
(or (and (number? (:x shape)) (not (sm/valid-safe-number? (:x shape)))) (fix-shape
(and (number? (:y shape)) (not (sm/valid-safe-number? (:y shape)))) [objects id shape]
(and (number? (:width shape)) (not (sm/valid-safe-number? (:width shape)))) (cond
(and (number? (:height shape)) (not (sm/valid-safe-number? (:height shape))))) (or (cfh/path-shape? shape)
(-> objects (cfh/bool-shape? shape))
(dissoc id) objects
(d/update-in-when [(:parent-id shape) :shapes]
(fn [shapes] (filterv #(not= id %) shapes))))
:else (or (and (number? (:x shape)) (not (sm/valid-safe-number? (:x shape))))
objects)) (and (number? (:y shape)) (not (sm/valid-safe-number? (:y shape))))
(and (number? (:width shape)) (not (sm/valid-safe-number? (:width shape))))
(and (number? (:height shape)) (not (sm/valid-safe-number? (:height shape)))))
(-> objects
(dissoc id)
(d/update-in-when [(:parent-id shape) :shapes]
(fn [shapes] (filterv #(not= id %) shapes))))
update-page :else
(fn [page] objects))]
(d/update-when page :objects #(reduce-kv update-object % %)))]
(-> file-data (-> file-data
(update :pages-index update-vals update-page) (update :pages-index update-vals fix-container)
(d/update-when :components update-vals update-page)))) (d/update-when :components update-vals fix-container))))
fix-misc-shape-issues fix-misc-shape-issues
(fn [file-data] (fn [file-data]
;; Find shapes that are not listed in their parent's children list. (letfn [(fix-container
;; Remove them, and also their children [container]
(let [update-shape (d/update-when container :objects update-vals fix-shape))
(fn [shape]
(cond-> shape
;; Some shapes has invalid value there
(contains? shape :layout-gap)
(d/update-in-when [:layout-gap :column-gap]
(fn [gap]
(if (or (= gap ##Inf)
(= gap ##-Inf))
0
gap)))
;; Fix broken fills (fix-shape
(seq (:fills shape)) [shape]
(update :fills (fn [fills] (filterv valid-fill? fills))) (cond-> shape
;; Some shapes has invalid gap value
(contains? shape :layout-gap)
(d/update-in-when [:layout-gap :column-gap]
(fn [gap]
(if (or (= gap ##Inf)
(= gap ##-Inf))
0
gap)))
;; Fix broken strokes ;; Fix broken fills
(seq (:strokes shape)) (seq (:fills shape))
(update :strokes (fn [strokes] (filterv valid-stroke? strokes))) (update :fills (fn [fills] (filterv valid-fill? fills)))
;; Fix some broken layout related attrs, probably ;; Fix broken strokes
;; of copypaste on flex layout betatest period (seq (:strokes shape))
(true? (:layout shape)) (update :strokes (fn [strokes] (filterv valid-stroke? strokes)))
(assoc :layout :flex)
(number? (:layout-gap shape)) ;; Fix some broken layout related attrs, probably
(as-> shape (let [n (:layout-gap shape)] ;; of copypaste on flex layout betatest period
(assoc shape :layout-gap {:row-gap n :column-gap n}))))) (true? (:layout shape))
(assoc :layout :flex)
update-container (number? (:layout-gap shape))
(fn [container] (as-> shape (let [n (:layout-gap shape)]
(d/update-when container :objects update-vals update-shape))] (assoc shape :layout-gap {:row-gap n :column-gap n})))))]
(-> file-data (-> file-data
(update :pages-index update-vals update-container) (update :pages-index update-vals fix-container)
(d/update-when :components update-vals update-container)))) (d/update-when :components update-vals fix-container))))
fix-recent-colors fix-recent-colors
(fn [file-data] (fn [file-data]
;; Remove invalid colors in :recent-colors
(d/update-when file-data :recent-colors (d/update-when file-data :recent-colors
(fn [colors] (fn [colors]
(filterv valid-color? colors)))) (filterv valid-color? colors))))
@ -489,7 +489,7 @@
(fix-misc-shape-issues) (fix-misc-shape-issues)
(fix-recent-colors) (fix-recent-colors)
(fix-missing-image-metadata) (fix-missing-image-metadata)
(fix-big-invalid-shapes) (delete-big-geometry-shapes)
(fix-orphan-shapes) (fix-orphan-shapes)
(fix-orphan-copies) (fix-orphan-copies)
(remove-nested-roots) (remove-nested-roots)