From 979828ffe3424503edf497543bf4afeabfa3dfa1 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 16 Sep 2024 09:08:51 +0200 Subject: [PATCH] :bug: Fix issue when exporting libraries when merging libraries --- CHANGES.md | 1 + frontend/src/app/worker/export.cljs | 143 ++++++++++------------------ 2 files changed, 51 insertions(+), 93 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 444c2db9c7..49b339c200 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -82,6 +82,7 @@ - Fix problem when creating a component instance from grid layout [Github #4881](https://github.com/penpot/penpot/issues/4881) - Fix problem when dismissing shared library update [Taiga #8669](https://tree.taiga.io/project/penpot/issue/8669) - Fix visual problem with stroke cap menu [Taiga #8730](https://tree.taiga.io/project/penpot/issue/8730) +- Fix issue when exporting libraries when merging libraries [Taiga #8758](https://tree.taiga.io/project/penpot/issue/8758) ## 2.1.5 diff --git a/frontend/src/app/worker/export.cljs b/frontend/src/app/worker/export.cljs index 2c346e7185..005108e687 100644 --- a/frontend/src/app/worker/export.cljs +++ b/frontend/src/app/worker/export.cljs @@ -202,108 +202,61 @@ (defn make-local-external-references [file file-id] - (let [detach-text + (let [change-fill + (fn [fill] + (cond-> fill + (not= file-id (:fill-color-ref-file fill)) + (assoc :fill-color-ref-file file-id))) + + change-stroke + (fn [stroke] + (cond-> stroke + (not= file-id (:stroke-color-ref-file stroke)) + (assoc :stroke-color-ref-file file-id))) + + change-text (fn [content] (->> content (ct/transform-nodes - #(cond-> % - (not= file-id (:fill-color-ref-file %)) - (assoc :fill-color-ref-file file-id) + (fn [node] + (-> node + (d/update-when :fills #(mapv change-fill %)) + (cond-> (not= file-id (:typography-ref-file node)) + (assoc :typography-ref-file file-id))))))) - (not= file-id (:typography-ref-file %)) - (assoc :typography-ref-file file-id))))) - - detach-shape + change-shape (fn [shape] - (cond-> shape - (not= file-id (:fill-color-ref-file shape)) - (assoc :fill-color-ref-file file-id) + (-> shape + (d/update-when :fills #(mapv change-fill %)) + (d/update-when :strokes #(mapv change-stroke %)) + (cond-> (not= file-id (:component-file shape)) + (assoc :component-file file-id)) - (not= file-id (:stroke-color-ref-file shape)) - (assoc :stroke-color-ref-file file-id) + (cond-> (= :text (:type shape)) + (update :content change-text)))) - (not= file-id (:component-file shape)) - (assoc :component-file file-id) - - (= :text (:type shape)) - (update :content detach-text))) - - detach-objects + change-objects (fn [objects] (->> objects - (d/mapm #(detach-shape %2)))) + (d/mapm #(change-shape %2)))) - detach-pages + change-pages (fn [pages-index] (->> pages-index (d/mapm (fn [_ data] (-> data - (update :objects detach-objects))))))] + (update :objects change-objects))))))] (-> file - (update-in [:data :pages-index] detach-pages)))) - -(defn collect-external-references - [file] - - (let [get-text-refs - (fn [content] - (->> content - (ct/node-seq #(or (contains? % :fill-color-ref-id) - (contains? % :typography-ref-id))) - - (mapcat (fn [node] - (cond-> [] - (contains? node :fill-color-ref-id) - (conj {:id (:fill-color-ref-id node) - :file-id (:fill-color-ref-file node)}) - - (contains? node :typography-ref-id) - (conj {:id (:typography-ref-id node) - :file-id (:typography-ref-file node)})))) - - (into []))) - - get-shape-refs - (fn [[_ shape]] - (cond-> [] - (contains? shape :fill-color-ref-id) - (conj {:id (:fill-color-ref-id shape) - :file-id (:fill-color-ref-file shape)}) - - (contains? shape :stroke-color-ref-id) - (conj {:id (:stroke-color-ref-id shape) - :file-id (:stroke-color-ref-file shape)}) - - (contains? shape :component-id) - (conj {:id (:component-id shape) - :file-id (:component-file shape)}) - - (= :text (:type shape)) - (into (get-text-refs (:content shape)))))] - - (->> (get-in file [:data :pages-index]) - (vals) - (mapcat :objects) - (mapcat get-shape-refs) - (filter (comp some? :file-id)) - (filter (comp some? :id)) - (group-by :file-id) - (d/mapm #(mapv :id %2))))) + (update-in [:data :pages-index] change-pages)))) (defn merge-assets [target-file assets-files] - (let [external-refs (collect-external-references target-file) - - merge-file-assets + (let [merge-file-assets (fn [target file] - (let [colors (-> (get-in file [:data :colors]) - (select-keys (get external-refs (:id file)))) - typographies (-> (get-in file [:data :typographies]) - (select-keys (get external-refs (:id file)))) - media (-> (get-in file [:data :media]) - (select-keys (get external-refs (:id file)))) - components (-> (ctkl/components (:data file)) - (select-keys (get external-refs (:id file))))] + (let [colors (get-in file [:data :colors]) + typographies (get-in file [:data :typographies]) + media (get-in file [:data :media]) + components (ctkl/components (:data file))] (cond-> target (d/not-empty? colors) (update-in [:data :colors] merge colors) @@ -323,16 +276,20 @@ (defn process-export [file-id export-type files] - (case export-type - :all files - :merge (let [file-list (-> files (d/without-keys [file-id]) vals)] - (-> (select-keys files [file-id]) - (update file-id merge-assets file-list) - (update file-id make-local-external-references file-id) - (update file-id dissoc :libraries))) - :detach (-> (select-keys files [file-id]) - (update file-id ctf/detach-external-references file-id) - (update file-id dissoc :libraries)))) + (let [result + (case export-type + :all files + :merge (let [file-list (-> files (d/without-keys [file-id]) vals)] + (-> (select-keys files [file-id]) + (update file-id merge-assets file-list) + (update file-id make-local-external-references file-id) + (update file-id dissoc :libraries))) + :detach (-> (select-keys files [file-id]) + (update file-id ctf/detach-external-references file-id) + (update file-id dissoc :libraries)))] + + ;;(.log js/console (clj->js result)) + result)) (defn collect-files [file-id export-type features]