From 88390432f5f7258f40a96830e52160ba52eb94ba Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Thu, 7 Sep 2023 12:40:54 +0200 Subject: [PATCH] :bug: Fix export file with components as basic objects --- backend/src/app/rpc/commands/binfile.clj | 10 ++-- common/src/app/common/types/file.cljc | 64 +++++++++++++++++++++++- frontend/src/app/worker/export.cljs | 60 +--------------------- 3 files changed, 71 insertions(+), 63 deletions(-) diff --git a/backend/src/app/rpc/commands/binfile.clj b/backend/src/app/rpc/commands/binfile.clj index c4a7efc5f..a2fd1d86a 100644 --- a/backend/src/app/rpc/commands/binfile.clj +++ b/backend/src/app/rpc/commands/binfile.clj @@ -15,6 +15,7 @@ [app.common.fressian :as fres] [app.common.logging :as l] [app.common.spec :as us] + [app.common.types.file :as ctf] [app.common.uuid :as uuid] [app.config :as cf] [app.db :as db] @@ -386,7 +387,6 @@ (def ^:dynamic *options* nil) ;; --- EXPORT WRITER - (defn- embed-file-assets [data cfg file-id] (letfn [(walk-map-form [form state] @@ -498,13 +498,17 @@ :hint "unable to retrieve files for export"))) (defmethod write-section :v1/files - [{:keys [::output ::embed-assets?] :as cfg}] + [{:keys [::output ::embed-assets? ::include-libraries?] :as cfg}] ;; Initialize SIDS with empty vector (vswap! *state* assoc :sids []) (doseq [file-id (-> *state* deref :files)] - (let [file (cond-> (get-file cfg file-id) + (let [detach? (and (not embed-assets?) (not include-libraries?)) + file (cond-> (get-file cfg file-id) + detach? + (-> (ctf/detach-external-references file-id) + (dissoc :libraries)) embed-assets? (update :data embed-file-assets cfg file-id)) diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 9a25dcd8d..4f4bbd394 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -12,8 +12,10 @@ [app.common.files.features :as ffeat] [app.common.geom.point :as gpt] [app.common.geom.shapes :as gsh] + [app.common.logging :as l] [app.common.pages.helpers :as cph] [app.common.schema :as sm] + [app.common.text :as ct] [app.common.types.color :as ctc] [app.common.types.colors-list :as ctcl] [app.common.types.component :as ctk] @@ -981,7 +983,7 @@ (report-error :missing-component-root (str/format "Referenced shape %s not found in near component" (:shape-ref shape)) shape file page)))) - + (defn validate-component-not-ref "Validate that this shape does not reference other one." [shape file page report-error] @@ -1072,7 +1074,7 @@ (defn validate-shape "Validate referential integrity and semantic coherence of a shape and all its children. - + The context is the situation of the parent in respect to components: :not-component :main-top @@ -1156,3 +1158,61 @@ (validate-shape-not-component shape file page libraries report-error)))))) @errors)) + +;; Export + +(defn- get-component-ref-file + [objects shape] + + (cond + (contains? shape :component-file) + (get shape :component-file) + + (contains? shape :shape-ref) + (recur objects (get objects (:parent-id shape))) + + :else + nil)) + +(defn detach-external-references + [file file-id] + (let [detach-text + (fn [content] + (->> content + (ct/transform-nodes + #(cond-> % + (not= file-id (:fill-color-ref-file %)) + (dissoc :fill-color-ref-id :fill-color-ref-file) + + (not= file-id (:typography-ref-file %)) + (dissoc :typography-ref-id :typography-ref-file))))) + + detach-shape + (fn [objects shape] + (l/debug :hint "detach-shape" + :file-id file-id + :component-ref-file (get-component-ref-file objects shape) + ::l/sync? true) + (cond-> shape + (not= file-id (:fill-color-ref-file shape)) + (dissoc :fill-color-ref-id :fill-color-ref-file) + + (not= file-id (:stroke-color-ref-file shape)) + (dissoc :stroke-color-ref-id :stroke-color-ref-file) + + (not= file-id (get-component-ref-file objects shape)) + (dissoc :component-id :component-file :shape-ref :component-root) + + (= :text (:type shape)) + (update :content detach-text))) + + detach-objects + (fn [objects] + (update-vals objects #(detach-shape objects %))) + + detach-pages + (fn [pages-index] + (update-vals pages-index #(update % :objects detach-objects)))] + + (-> file + (update-in [:data :pages-index] detach-pages)))) diff --git a/frontend/src/app/worker/export.cljs b/frontend/src/app/worker/export.cljs index aef836849..9cf7aaf6d 100644 --- a/frontend/src/app/worker/export.cljs +++ b/frontend/src/app/worker/export.cljs @@ -10,6 +10,7 @@ [app.common.media :as cm] [app.common.text :as ct] [app.common.types.components-list :as ctkl] + [app.common.types.file :as ctf] [app.config :as cfg] [app.main.render :as r] [app.main.repo :as rp] @@ -170,63 +171,6 @@ (let [libraries-ids (->> file-libraries (map :id) (filterv #(not= (:id file) %)))] (assoc file :libraries libraries-ids))))))) -(defn get-component-ref-file - [objects shape] - - (cond - (contains? shape :component-file) - (get shape :component-file) - - (contains? shape :shape-ref) - (recur objects (get objects (:parent-id shape))) - - :else - nil)) - -(defn detach-external-references - [file file-id] - (let [detach-text - (fn [content] - (->> content - (ct/transform-nodes - #(cond-> % - (not= file-id (:fill-color-ref-file %)) - (dissoc :fill-color-ref-id :fill-color-ref-file) - - (not= file-id (:typography-ref-file %)) - (dissoc :typography-ref-id :typography-ref-file))))) - - detach-shape - (fn [objects shape] - (cond-> shape - (not= file-id (:fill-color-ref-file shape)) - (dissoc :fill-color-ref-id :fill-color-ref-file) - - (not= file-id (:stroke-color-ref-file shape)) - (dissoc :stroke-color-ref-id :stroke-color-ref-file) - - (not= file-id (get-component-ref-file objects shape)) - (dissoc :component-id :component-file :shape-ref :component-root) - - (= :text (:type shape)) - (update :content detach-text))) - - detach-objects - (fn [objects] - (->> objects - (d/mapm #(detach-shape objects %2)))) - - detach-pages - (fn [pages-index] - (->> pages-index - (d/mapm - (fn [_ data] - (-> data - (update :objects detach-objects))))))] - - (-> file - (update-in [:data :pages-index] detach-pages)))) - (defn make-local-external-references [file file-id] (let [detach-text @@ -359,7 +303,7 @@ (update file-id make-local-external-references file-id) (update file-id dissoc :libraries))) :detach (-> (select-keys files [file-id]) - (update file-id detach-external-references file-id) + (update file-id ctf/detach-external-references file-id) (update file-id dissoc :libraries)))) (defn collect-files