From 4c7e565f6ad4a8167481da3d8fd1963a9c031ffc Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Thu, 18 Jan 2024 12:45:48 +0100 Subject: [PATCH] :bug: Fix keep layout-item properties after swap --- common/src/app/common/types/component.cljc | 23 ++++++++++--------- .../app/main/data/workspace/libraries.cljs | 18 +++++++++++---- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/common/src/app/common/types/component.cljc b/common/src/app/common/types/component.cljc index 9dd0a8c80..67ffd2414 100644 --- a/common/src/app/common/types/component.cljc +++ b/common/src/app/common/types/component.cljc @@ -83,18 +83,19 @@ :layout-grid-dir :layout-container :layout-grid-rows :layout-container :layout-grid-columns :layout-container - :layout-grid-cells :layout-container - - :layout-item-margin :layout-item - :layout-item-margin-type :layout-item - :layout-item-h-sizing :layout-item - :layout-item-v-sizing :layout-item - :layout-item-max-h :layout-item - :layout-item-min-h :layout-item - :layout-item-max-w :layout-item - :layout-item-min-w :layout-item - :layout-item-align-self :layout-item}) + :layout-grid-cells :layout-container}) +(def swap-keep-attrs + [:layout-item-margin + :layout-item-margin-type + :layout-item-h-sizing + :layout-item-v-sizing + :layout-item-max-h + :layout-item-min-h + :layout-item-max-w + :layout-item-min-w + :layout-item-absolute + :layout-item-z-index]) (defn instance-root? "Check if this shape is the head of a top instance." diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 30106467d..2a7b7984d 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -829,7 +829,7 @@ 0))))) (defn- add-component-for-swap - [shape file-id id-new-component index target-cell] + [shape file-id id-new-component index target-cell keep-props-values] (dm/assert! (uuid? id-new-component)) (dm/assert! (uuid? file-id)) (ptk/reify ::add-component-for-swap @@ -856,8 +856,13 @@ (:parent-id shape) (:frame-id shape)) - ;; We need to set the same index as the original shape - changes (pcb/change-parent changes (:parent-id shape) [new-shape] index {:component-swap true})] + changes + (-> changes + ;; Restore the properties + (pcb/update-shapes [(:id new-shape)] #(d/patch-object % keep-props-values)) + + ;; We need to set the same index as the original shape + (pcb/change-parent (:parent-id shape) [new-shape] index {:component-swap true}))] ;; First delete so we don't break the grid layout cells (rx/of (dch/commit-changes changes) @@ -880,9 +885,12 @@ target-cell (when (ctl/grid-layout? parent) (ctl/get-cell-by-shape-id parent (:id shape))) - index (find-shape-index objects (:parent-id shape) (:id shape))] + index (find-shape-index objects (:parent-id shape) (:id shape)) + + ;; Store the properties that need to be maintained when the component is swapped + keep-props-values (select-keys shape ctk/swap-keep-attrs)] (rx/of (dwsh/delete-shapes nil (d/ordered-set (:id shape)) {:component-swap true}) - (add-component-for-swap shape file-id id-new-component index target-cell) + (add-component-for-swap shape file-id id-new-component index target-cell keep-props-values) (ptk/data-event :layout/update [(:parent-id shape)])))))) (defn component-multi-swap