🐛 Fix problem with guides when duplicating components

This commit is contained in:
alonso.torres 2024-02-05 10:45:52 +01:00 committed by Andrey Antukh
parent 769aa16cc4
commit 6f48f8eceb
2 changed files with 45 additions and 28 deletions

View file

@ -393,6 +393,9 @@
unames (volatile! (cfh/get-used-names (:objects page))) unames (volatile! (cfh/get-used-names (:objects page)))
update-unames! (fn [new-name] (vswap! unames conj new-name)) 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) 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) ids-map (into {} (map #(vector % (uuid/next))) all-ids)
changes changes
@ -409,7 +412,15 @@
library-data library-data
it it
file-id) 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 (-> changes
(prepare-duplicate-flows shapes page ids-map) (prepare-duplicate-flows shapes page ids-map)
@ -578,27 +589,29 @@
(defn- prepare-duplicate-guides (defn- prepare-duplicate-guides
[changes shapes page ids-map delta] [changes shapes page ids-map delta]
(let [guides (get-in page [:options :guides]) (let [guides (get-in page [:options :guides])
frames (->> shapes frames (->> shapes (filter cfh/frame-shape?))
(filter #(= (:type %) :frame)))
new-guides (reduce new-guides
(fn [g frame] (reduce
(let [new-id (ids-map (:id frame)) (fn [g frame]
new-frame (-> frame (let [new-id (ids-map (:id frame))
(gsh/move delta)) new-frame (-> frame (gsh/move delta))
new-guides (->> guides
(vals) new-guides
(filter #(= (:frame-id %) (:id frame))) (->> guides
(map #(-> % (vals)
(assoc :id (uuid/next)) (filter #(= (:frame-id %) (:id frame)))
(assoc :frame-id new-id) (map #(-> %
(assoc :position (if (= (:axis %) :x) (assoc :id (uuid/next))
(+ (:position %) (- (:x new-frame) (:x frame))) (assoc :frame-id new-id)
(+ (:position %) (- (:y new-frame) (:y frame))))))))] (assoc :position (if (= (:axis %) :x)
(cond-> g (+ (:position %) (- (:x new-frame) (:x frame)))
(not-empty new-guides) (+ (:position %) (- (:y new-frame) (:y frame))))))))]
(conj (into {} (map (juxt :id identity) new-guides)))))) (cond-> g
guides (not-empty new-guides)
frames)] (conj (into {} (map (juxt :id identity) new-guides))))))
guides
frames)]
(-> (pcb/with-page changes page) (-> (pcb/with-page changes page)
(pcb/set-page-option :guides new-guides)))) (pcb/set-page-option :guides new-guides))))

View file

@ -178,12 +178,16 @@
interactions))) interactions)))
(vals objects)) (vals objects))
;; If any of the deleted shapes is a frame with guides ids-set (set ids)
guides (into {} guides-to-remove
(comp (map second) (->> (dm/get-in page [:options :guides])
(remove #(contains? ids (:frame-id %))) (vals)
(map (juxt :id identity))) (filter #(contains? ids-set (:frame-id %)))
(dm/get-in page [:options :guides])) (map :id))
guides
(->> guides-to-remove
(reduce dissoc (dm/get-in page [:options :guides])))
starting-flows starting-flows
(filter (fn [flow] (filter (fn [flow]