From 6f48f8ecebffcbe0f21818140c147c24cac2d3ec Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Mon, 5 Feb 2024 10:45:52 +0100 Subject: [PATCH] :bug: Fix problem with guides when duplicating components --- .../app/main/data/workspace/selection.cljs | 57 ++++++++++++------- .../src/app/main/data/workspace/shapes.cljs | 16 ++++-- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/frontend/src/app/main/data/workspace/selection.cljs b/frontend/src/app/main/data/workspace/selection.cljs index 7c6aecea5..033c96000 100644 --- a/frontend/src/app/main/data/workspace/selection.cljs +++ b/frontend/src/app/main/data/workspace/selection.cljs @@ -393,6 +393,9 @@ unames (volatile! (cfh/get-used-names (:objects page))) update-unames! (fn [new-name] (vswap! unames conj new-name)) all-ids (reduce #(into %1 (cons %2 (cfh/get-children-ids all-objects %2))) (d/ordered-set) ids) + + ;; We need ids-map for remapping the grid layout. But when duplicating the guides + ;; we calculate a new one because the components will have created new shapes. ids-map (into {} (map #(vector % (uuid/next))) all-ids) changes @@ -409,7 +412,15 @@ library-data it file-id) - init-changes))] + init-changes)) + + ;; We need to check the changes to get the ids-map + ids-map + (into {} + (comp + (filter #(= :add-obj (:type %))) + (map #(vector (:old-id %) (-> % :obj :id)))) + (:redo-changes changes))] (-> changes (prepare-duplicate-flows shapes page ids-map) @@ -578,27 +589,29 @@ (defn- prepare-duplicate-guides [changes shapes page ids-map delta] (let [guides (get-in page [:options :guides]) - frames (->> shapes - (filter #(= (:type %) :frame))) - new-guides (reduce - (fn [g frame] - (let [new-id (ids-map (:id frame)) - new-frame (-> frame - (gsh/move delta)) - new-guides (->> guides - (vals) - (filter #(= (:frame-id %) (:id frame))) - (map #(-> % - (assoc :id (uuid/next)) - (assoc :frame-id new-id) - (assoc :position (if (= (:axis %) :x) - (+ (:position %) (- (:x new-frame) (:x frame))) - (+ (:position %) (- (:y new-frame) (:y frame))))))))] - (cond-> g - (not-empty new-guides) - (conj (into {} (map (juxt :id identity) new-guides)))))) - guides - frames)] + frames (->> shapes (filter cfh/frame-shape?)) + + new-guides + (reduce + (fn [g frame] + (let [new-id (ids-map (:id frame)) + new-frame (-> frame (gsh/move delta)) + + new-guides + (->> guides + (vals) + (filter #(= (:frame-id %) (:id frame))) + (map #(-> % + (assoc :id (uuid/next)) + (assoc :frame-id new-id) + (assoc :position (if (= (:axis %) :x) + (+ (:position %) (- (:x new-frame) (:x frame))) + (+ (:position %) (- (:y new-frame) (:y frame))))))))] + (cond-> g + (not-empty new-guides) + (conj (into {} (map (juxt :id identity) new-guides)))))) + guides + frames)] (-> (pcb/with-page changes page) (pcb/set-page-option :guides new-guides)))) diff --git a/frontend/src/app/main/data/workspace/shapes.cljs b/frontend/src/app/main/data/workspace/shapes.cljs index 5288239ac..99e13369a 100644 --- a/frontend/src/app/main/data/workspace/shapes.cljs +++ b/frontend/src/app/main/data/workspace/shapes.cljs @@ -178,12 +178,16 @@ interactions))) (vals objects)) - ;; If any of the deleted shapes is a frame with guides - guides (into {} - (comp (map second) - (remove #(contains? ids (:frame-id %))) - (map (juxt :id identity))) - (dm/get-in page [:options :guides])) + ids-set (set ids) + guides-to-remove + (->> (dm/get-in page [:options :guides]) + (vals) + (filter #(contains? ids-set (:frame-id %))) + (map :id)) + + guides + (->> guides-to-remove + (reduce dissoc (dm/get-in page [:options :guides]))) starting-flows (filter (fn [flow]