🐛 Fix bad selection after variant duplicate

This commit is contained in:
Pablo Alba 2025-09-08 16:33:38 +02:00
parent 9f11a2cb32
commit c5bd183f73

View File

@ -296,48 +296,52 @@
(defn add-new-variant (defn add-new-variant
"Create a new variant and add it to the variant-container" "Create a new variant and add it to the variant-container"
[shape-id] ([shape-id]
(ptk/reify ::add-new-variant (add-new-variant shape-id false))
ptk/WatchEvent ([shape-id multiselect?]
(watch [it state _] (ptk/reify ::add-new-variant
(let [page-id (:current-page-id state) ptk/WatchEvent
data (dsh/lookup-file-data state) (watch [it state _]
objects (-> (dsh/get-page data page-id) (let [page-id (:current-page-id state)
(get :objects)) data (dsh/lookup-file-data state)
shape (get objects shape-id) objects (-> (dsh/get-page data page-id)
shape (if (ctc/is-variant-container? shape) (get :objects))
(get objects (last (:shapes shape))) shape (get objects shape-id)
shape) shape (if (ctc/is-variant-container? shape)
component-id (:component-id shape) (get objects (last (:shapes shape)))
component (ctkl/get-component data component-id) shape)
component-id (:component-id shape)
component (ctkl/get-component data component-id)
container-id (:parent-id shape) container-id (:parent-id shape)
variant-container (get objects container-id) variant-container (get objects container-id)
has-layout? (ctsl/any-layout? variant-container) has-layout? (ctsl/any-layout? variant-container)
new-component-id (uuid/next) new-component-id (uuid/next)
new-shape-id (uuid/next) new-shape-id (uuid/next)
prop-num (dec (count (:variant-properties component))) prop-num (dec (count (:variant-properties component)))
changes (-> (pcb/empty-changes it page-id) changes (-> (pcb/empty-changes it page-id)
(pcb/with-library-data data) (pcb/with-library-data data)
(pcb/with-objects objects) (pcb/with-objects objects)
(pcb/with-page-id page-id) (pcb/with-page-id page-id)
(clv/generate-add-new-variant shape (:variant-id component) new-component-id new-shape-id prop-num)) (clv/generate-add-new-variant shape (:variant-id component) new-component-id new-shape-id prop-num))
undo-id (js/Symbol)] undo-id (js/Symbol)]
(rx/concat (rx/concat
(rx/of (rx/of
(dwu/start-undo-transaction undo-id) (dwu/start-undo-transaction undo-id)
(dch/commit-changes changes) (dch/commit-changes changes)
(when-not has-layout? (when-not has-layout?
(resposition-and-resize-variant new-shape-id)) (resposition-and-resize-variant new-shape-id))
(dwu/commit-undo-transaction undo-id) (dwu/commit-undo-transaction undo-id)
(ptk/data-event :layout/update {:ids [(:parent-id shape)]}) (ptk/data-event :layout/update {:ids [(:parent-id shape)]})
(dws/select-shape new-shape-id)) (if multiselect?
(->> (rx/of (focus-property new-shape-id prop-num)) (dws/shift-select-shapes new-shape-id)
(rx/delay 250))))))) (dws/select-shape new-shape-id)))
(->> (rx/of (focus-property new-shape-id prop-num))
(rx/delay 250))))))))
(defn transform-in-variant (defn transform-in-variant
"Given the id of a main shape of a component, creates a variant structure for "Given the id of a main shape of a component, creates a variant structure for
@ -491,8 +495,9 @@
(rx/concat (rx/concat
(rx/of (rx/of
(ptk/event ::ev/event {::ev/name "add-new-variant" :trigger "shortcut-duplicate"}) (ptk/event ::ev/event {::ev/name "add-new-variant" :trigger "shortcut-duplicate"})
(dwu/start-undo-transaction undo-id)) (dwu/start-undo-transaction undo-id)
(rx/from (map add-new-variant selected-ids)) (add-new-variant (first selected-ids) false))
(rx/from (map #(add-new-variant % true) (rest selected-ids)))
(rx/of (dwu/commit-undo-transaction undo-id))) (rx/of (dwu/commit-undo-transaction undo-id)))
(rx/of (dws/duplicate-selected true))))))) (rx/of (dws/duplicate-selected true)))))))