🐛 Fix layer orders messed up on move, group, reparent and undo

This commit is contained in:
Pablo Alba 2023-01-04 08:32:14 +01:00 committed by Andrés Moya
parent 3480d6979b
commit 987b7f44f4
9 changed files with 57 additions and 33 deletions

View file

@ -639,14 +639,15 @@
(defn relocate-shapes-changes [it objects parents parent-id page-id to-index ids
groups-to-delete groups-to-unmask shapes-to-detach
shapes-to-reroot shapes-to-deroot shapes-to-unconstraint]
(let [shapes (map (d/getf objects) ids)]
(let [ordered-indexes (cph/order-by-indexed-shapes objects ids)
shapes (map (d/getf objects) ordered-indexes)]
(-> (pcb/empty-changes it page-id)
(pcb/with-objects objects)
; Move the shapes
(pcb/change-parent parent-id
(reverse shapes)
shapes
to-index)
; Remove empty groups

View file

@ -20,15 +20,11 @@
[cuerdas.core :as str]
[potok.core :as ptk]))
(defn selected-shapes
(defn selected-shapes-idx
[state]
(let [objects (wsh/lookup-page-objects state)]
(->> (wsh/lookup-selected state)
(cph/clean-loops objects)
(map (d/getf objects))
(remove cph/frame-shape?)
(map #(assoc % ::index (cph/get-position-on-parent objects (:id %))))
(sort-by ::index))))
(cph/clean-loops objects))))
(defn create-bool-data
[bool-type name shapes objects]
@ -92,10 +88,15 @@
base-name (-> bool-type d/name str/capital (str "-1"))
name (-> (ctt/retrieve-used-names objects)
(ctt/generate-unique-name base-name))
shapes (selected-shapes state)]
ids (selected-shapes-idx state)
ordered-indexes (cph/order-by-indexed-shapes objects ids)
shapes (->> ordered-indexes
(map (d/getf objects))
(remove cph/frame-shape?))]
(when-not (empty? shapes)
(let [[boolean-data index] (create-bool-data bool-type name shapes objects)
(let [[boolean-data index] (create-bool-data bool-type name (reverse shapes) objects)
index (inc index)
shape-id (:id boolean-data)
changes (-> (pcb/empty-changes it page-id)
(pcb/with-objects objects)

View file

@ -22,6 +22,7 @@
(defn shapes-for-grouping
[objects selected]
(->> selected
(cph/order-by-indexed-shapes objects)
(map #(get objects %))
(map #(assoc % ::index (cph/get-position-on-parent objects (:id %))))
(sort-by ::index)))
@ -76,12 +77,13 @@
(ctst/generate-unique-name base-name)))
selrect (gsh/selection-rect shapes)
group-idx (inc (::index (last shapes)))
group (-> (cts/make-minimal-group frame-id selrect gname)
(cts/setup-shape selrect)
(assoc :shapes (mapv :id shapes)
:parent-id parent-id
:frame-id frame-id
:index (::index (first shapes))))
:index group-idx))
;; Shapes that are in a component, but are not root, must be detached,
;; because they will be now children of a non instance group.
@ -93,8 +95,8 @@
changes (-> (pcb/empty-changes it page-id)
(pcb/with-objects objects)
(pcb/add-object group {:index (::index (first shapes))})
(pcb/change-parent (:id group) shapes)
(pcb/add-object group {:index group-idx})
(pcb/change-parent (:id group) (reverse shapes))
(pcb/update-shapes (map :id shapes-to-detach) ctk/detach-shape)
(pcb/remove-objects ids-to-delete))]
@ -102,7 +104,9 @@
(defn remove-group-changes
[it page-id group objects]
(let [children (mapv #(get objects %) (:shapes group))
(let [children (->> (:shapes group)
(cph/order-by-indexed-shapes objects)
(mapv #(get objects %)))
parent-id (cph/get-parent-id objects (:id group))
parent (get objects parent-id)
@ -114,7 +118,7 @@
;; Shapes that are in a component (including root) must be detached,
;; because cannot be easyly synchronized back to the main component.
shapes-to-detach (filter ctk/in-component-instance?
shapes-to-detach (filter ctk/in-component-instance?
(cph/get-children-with-self objects (:id group)))]
(-> (pcb/empty-changes it page-id)
@ -125,8 +129,9 @@
(defn remove-frame-changes
[it page-id frame objects]
(let [children (mapv #(get objects %) (:shapes frame))
(let [children (->> (:shapes frame)
(cph/order-by-indexed-shapes objects)
(mapv #(get objects %)))
parent-id (cph/get-parent-id objects (:id frame))
idx-in-parent (cph/get-position-on-parent objects (:id frame))]

View file

@ -118,10 +118,8 @@
(let [page-id (:current-page-id state)
objects (wsh/lookup-page-objects state page-id)
to-move-shapes
(into []
(map (d/getf objects))
(reverse (ctst/sort-z-index objects shapes)))
ordered-indexes (cph/order-by-indexed-shapes objects shapes)
to-move-shapes (map (d/getf objects) ordered-indexes)
changes
(when (d/not-empty? to-move-shapes)

View file

@ -685,6 +685,7 @@
shapes (->> ids (cph/clean-loops objects) (keep lookup))
moving-shapes
(cond->> shapes
(not layout?)
@ -694,6 +695,9 @@
(remove #(and (= (:frame-id %) frame-id)
(not= (:parent-id %) frame-id))))
ordered-indexes (cph/order-by-indexed-shapes objects (map :id moving-shapes))
moving-shapes (map (d/getf objects) ordered-indexes)
all-parents
(reduce (fn [res id]
(into res (cph/get-parent-ids objects id)))