From b7d7cf233ac68943866cb2800b1c0075932f6a39 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 22 Apr 2025 19:58:10 +0200 Subject: [PATCH] :sparkles: Fix shadow colors on import penpot files --- backend/src/app/binfile/cleaner.clj | 44 +++++++++++++++++++++++++++++ backend/src/app/binfile/v3.clj | 32 ++++++++++++++------- 2 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 backend/src/app/binfile/cleaner.clj diff --git a/backend/src/app/binfile/cleaner.clj b/backend/src/app/binfile/cleaner.clj new file mode 100644 index 0000000000..1f4d29ea1f --- /dev/null +++ b/backend/src/app/binfile/cleaner.clj @@ -0,0 +1,44 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS INC + +(ns app.binfile.cleaner + "A collection of helpers for perform cleaning of artifacts; mainly + for recently imported shapes." + (:require + [app.common.data :as d] + [app.common.uuid :as uuid])) + +(defn- fix-shape-shadow-color + "Some shapes can come with invalid `id` property on shadow colors + caused by incorrect uuid parsing bug that should be already fixed; + this function removes the invalid id from the data structure." + [shape] + (let [fix-color + (fn [{:keys [id] :as color}] + (if (uuid? id) + color + (if (and (string? id) + (re-matches uuid/regex id)) + (assoc color :id (uuid/uuid id)) + (dissoc color :id)))) + + fix-shadow + (fn [shadow] + (d/update-when shadow :color fix-color)) + + xform + (map fix-shadow)] + + (d/update-when shape :shadow + (fn [shadows] + (into [] xform shadows))))) + +(defn clean-shape-post-decode + "A shape procesor that expected to be executed after schema decoding + process but before validation." + [shape] + (-> shape + (fix-shape-shadow-color))) diff --git a/backend/src/app/binfile/v3.clj b/backend/src/app/binfile/v3.clj index dc6bf7b80b..07e953d68d 100644 --- a/backend/src/app/binfile/v3.clj +++ b/backend/src/app/binfile/v3.clj @@ -8,6 +8,7 @@ "A ZIP based binary file exportation" (:refer-clojure :exclude [read]) (:require + [app.binfile.cleaner :as bfl] [app.binfile.common :as bfc] [app.binfile.migrations :as bfm] [app.common.data :as d] @@ -594,16 +595,25 @@ (defn- read-file-components [{:keys [::bfc/input ::file-id ::entries]}] - (->> (keep (match-component-entry-fn file-id) entries) - (reduce (fn [result {:keys [id entry]}] - (let [object (->> (read-entry input entry) - (decode-component) - (validate-component))] - (if (= id (:id object)) - (assoc result id object) - result))) - {}) - (not-empty))) + (let [clean-component-post-decode + (fn [component] + (d/update-when component :objects + (fn [objects] + (reduce-kv (fn [objects id shape] + (assoc objects id (bfl/clean-shape-post-decode shape))) + objects + objects))))] + (->> (keep (match-component-entry-fn file-id) entries) + (reduce (fn [result {:keys [id entry]}] + (let [object (->> (read-entry input entry) + (decode-component) + (clean-component-post-decode) + (validate-component))] + (if (= id (:id object)) + (assoc result id object) + result))) + {}) + (not-empty)))) (defn- read-file-typographies [{:keys [::bfc/input ::file-id ::entries]}] @@ -631,7 +641,9 @@ (reduce (fn [result {:keys [id entry]}] (let [object (->> (read-entry input entry) (decode-shape) + (bfl/clean-shape-post-decode) (validate-shape))] + (if (= id (:id object)) (assoc result id object) result)))