From b47df2c230d9a8255eac272957e002fcec90f7ca Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Thu, 6 Mar 2025 10:38:25 +0100 Subject: [PATCH] :bug: Fix problem with components and grid layout --- .../src/app/common/files/changes_builder.cljc | 2 +- common/src/app/common/logic/libraries.cljc | 152 ++++++++++-------- common/src/app/common/types/shape/layout.cljc | 18 ++- 3 files changed, 95 insertions(+), 77 deletions(-) diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index fe27e4c81..bf1b7b0bc 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -154,7 +154,7 @@ (let [data (::file-data (meta changes))] (dm/get-in data [:pages-index uuid/zero :objects]))) -(defn- apply-changes-local +(defn apply-changes-local [changes] (dm/assert! "expected valid changes" diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index be9c482b5..5ad85cbcb 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -747,42 +747,35 @@ (let [omit-touched? (not reset?) clear-remote-synced? (and initial-root? reset?) set-remote-synced? (and (not initial-root?) reset?) - changes (cond-> changes - :always - (update-attrs shape-inst - shape-main - root-inst - root-main - container - omit-touched?) + changes + (cond-> changes + :always + (update-attrs shape-inst + shape-main + root-inst + root-main + container + omit-touched?) - (ctl/flex-layout? shape-main) - (update-flex-child-copy-attrs shape-main - shape-inst - library - component - container - omit-touched?) + (ctl/flex-layout? shape-main) + (update-flex-child-copy-attrs shape-main + shape-inst + library + component + container + omit-touched?) - (ctl/grid-layout? shape-main) - (update-grid-copy-attrs shape-main - shape-inst - library - component - container - omit-touched?) + reset? + (change-touched shape-inst + shape-main + container + {:reset-touched? true}) - reset? - (change-touched shape-inst - shape-main - container - {:reset-touched? true}) + clear-remote-synced? + (change-remote-synced shape-inst container nil) - clear-remote-synced? - (change-remote-synced shape-inst container nil) - - set-remote-synced? - (change-remote-synced shape-inst container true)) + set-remote-synced? + (change-remote-synced shape-inst container true)) component-container (find-main-container container shape-inst shape-main library component) @@ -859,23 +852,36 @@ (d/index-of children-inst child-inst) (d/index-of children-main child-main) container - omit-touched?))] + omit-touched?)) - (compare-children changes - children-inst - children-main - container - component-container - file - libraries - only-inst - only-main - both - swapped - moved - false - reset? - components-v2)))) + changes + (compare-children changes + children-inst + children-main + container + component-container + file + libraries + only-inst + only-main + both + swapped + moved + false + reset? + components-v2) + + changes + (cond-> changes + (ctl/grid-layout? shape-inst) + (update-grid-copy-attrs + (:id shape-inst) + shape-main + library + component + omit-touched?))] + + changes))) (defn generate-rename-component "Generate the changes for rename the component with the given id, in the current file library." @@ -1710,30 +1716,36 @@ (defn- update-grid-copy-attrs "Synchronizes the `layout-grid-cells` property from the main shape to the copies" - [changes shape-main shape-copy main-container main-component copy-container omit-touched?] - (let [ids-map - (into {} - (comp - (map #(dm/get-in copy-container [:objects %])) - (keep - (fn [copy-shape] - (let [main-shape (ctf/get-ref-shape main-container main-component copy-shape)] - [(:id main-shape) (:id copy-shape)])))) - (:shapes shape-copy)) + [changes shape-copy-id shape-main main-container main-component omit-touched?] + (-> changes + (pcb/apply-changes-local) + (pcb/update-shapes + [shape-copy-id] + (fn [shape-copy objects] + (let [ids-map + (into {} + (comp + (map #(get objects %)) + (keep + (fn [copy-shape] + (let [main-shape (ctf/get-ref-shape main-container main-component copy-shape)] + [(:id main-shape) (:id copy-shape)])))) + (:shapes shape-copy)) - new-changes - (-> (pcb/empty-changes) - (pcb/with-container copy-container) - (pcb/with-objects (:objects copy-container)) - (pcb/update-shapes - [(:id shape-copy)] - (fn [shape-copy] + remove-orphan-cells + (fn [cells {:keys [shapes]}] + (let [child? (set shapes)] + (-> cells + (update-vals + (fn [cell] + (update cell :shapes #(filterv child? %))))))) ;; Take cells from main and remap the shapes to assign it to the copy - (let [copy-cells (:layout-grid-cells shape-copy) - main-cells (-> (ctl/remap-grid-cells shape-main ids-map) :layout-grid-cells)] - (assoc shape-copy :layout-grid-cells (ctl/merge-cells copy-cells main-cells omit-touched?)))) - {:ignore-touched true}))] - (pcb/concat-changes changes new-changes))) + copy-cells (-> shape-copy :layout-grid-cells (remove-orphan-cells shape-copy)) + main-cells (-> shape-main (ctl/remap-grid-cells ids-map) :layout-grid-cells)] + (-> shape-copy + (assoc :layout-grid-cells + (ctl/merge-cells copy-cells main-cells omit-touched?))))) + {:ignore-touched true :with-objects? true}))) (defn- update-grid-main-attrs "Synchronizes the `layout-grid-cells` property from the copy to the main shape" diff --git a/common/src/app/common/types/shape/layout.cljc b/common/src/app/common/types/shape/layout.cljc index 423e2a4d2..9c929de7a 100644 --- a/common/src/app/common/types/shape/layout.cljc +++ b/common/src/app/common/types/shape/layout.cljc @@ -1642,11 +1642,16 @@ "Given target cells update with source cells while trying to keep target as untouched as possible" [target-cells source-cells omit-touched?] - (if (not omit-touched?) - source-cells - + (if omit-touched? (letfn [(get-data [cells id] - (dissoc (get cells id) :shapes :row :column :row-span :column-span))] + (dissoc (get cells id) :row :column :row-span :column-span)) + + (merge-cells [source-cell target-cell] + (-> source-cell + (d/patch-object + (dissoc target-cell :shapes :row :column :row-span :column-span)) + (cond-> (d/not-empty? (:shapes target-cell)) + (assoc :shapes (:shapes target-cell)))))] (let [deleted-cells (into #{} (filter #(not (contains? source-cells %))) @@ -1664,5 +1669,6 @@ (reduce (fn [cells id] (-> cells - (d/update-when id d/patch-object (get-data target-cells id)))) - source-cells)))))) + (d/update-when id merge-cells (get target-cells id)))) + source-cells)))) + source-cells))