0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 07:50:43 -05:00

🐛 Fix problem with absolutes inside grid

This commit is contained in:
alonso.torres 2023-12-15 10:22:54 +01:00
parent f8dd86da34
commit 52c849ce4b
10 changed files with 57 additions and 46 deletions

View file

@ -358,13 +358,13 @@
(defn changed-attrs
"Returns the list of attributes that will change when `update-fn` is applied"
[object update-fn {:keys [attrs]}]
[object objects update-fn {:keys [attrs]}]
(let [changed?
(fn [old new attr]
(let [old-val (get old attr)
new-val (get new attr)]
(not= old-val new-val)))
new-obj (update-fn object)]
new-obj (update-fn object objects)]
(when-not (= object new-obj)
(let [attrs (or attrs (d/concat-set (keys object) (keys new-obj)))]
(filter (partial changed? object new-obj) attrs)))))
@ -412,7 +412,7 @@
update-shape
(fn [changes id]
(let [old-obj (get objects id)
new-obj (update-fn old-obj)]
new-obj (update-fn old-obj objects)]
(if (= old-obj new-obj)
changes
(let [[rops uops] (-> (or attrs (d/concat-set (keys old-obj) (keys new-obj)))

View file

@ -676,7 +676,7 @@
[id cell])))
(defn remove-grid-column
[parent index]
[parent index objects]
(let [track-num (inc index)
@ -692,10 +692,10 @@
(-> parent
(update :layout-grid-columns d/remove-at-index index)
(update :layout-grid-cells update-cells)
(assign-cells))))
(assign-cells objects))))
(defn remove-grid-row
[parent index]
[parent index objects]
(let [track-num (inc index)
decrease-track-num (make-decrease-track-num :row :row-span track-num)
@ -710,7 +710,7 @@
(-> parent
(update :layout-grid-rows d/remove-at-index index)
(update :layout-grid-cells update-cells)
(assign-cells))))
(assign-cells objects))))
(defn- reorder-grid-tracks
"Swap the positions of the tracks info"
@ -781,6 +781,7 @@
parent
(reorder-grid-tracks parent tracks-props from-index to-index)]
(cond-> parent
move-content?
(swap-track-content prop from-track to-track))))
@ -828,14 +829,24 @@
(defn check-deassigned-cells
"Clean the cells whith shapes that are no longer in the layout"
[parent]
[parent objects]
(let [child? (set (:shapes parent))
cells (update-vals
(:layout-grid-cells parent)
(fn [cell] (update cell :shapes #(filterv child? %))))]
(let [child-set (set (:shapes parent))
(assoc parent :layout-grid-cells cells)))
assigned?
(fn [id]
(and (contains? child-set id)
(not (position-absolute? objects id))))
cells
(update-vals
(:layout-grid-cells parent)
(fn [cell]
(-> cell
(update :shapes #(filterv assigned? %)))))]
(-> parent
(assoc :layout-grid-cells cells))))
(defn overlapping-cells
"Find overlapping cells"
@ -902,12 +913,12 @@
;; - Shape duplication
;; - (maybe) create group/frames. This case will assigna a cell that had one of its children
(defn assign-cells
[parent]
[parent objects]
(let [;; TODO: Remove this, shouldn't be happening
;;overlaps (overlapping-cells parent)
;;_ (when (not (empty? overlaps))
;; (.warn js/console "OVERLAPS" overlaps))
parent (cond-> (check-deassigned-cells parent)
parent (cond-> (check-deassigned-cells parent objects)
#_(d/not-empty? overlaps)
#_(fix-overlaps overlaps))
@ -915,7 +926,9 @@
(into #{} (mapcat (comp :shapes second)) (:layout-grid-cells parent))
no-cell-shapes
(->> (:shapes parent) (remove shape-has-cell?))
(->> (:shapes parent)
(remove shape-has-cell?)
(remove (partial position-absolute? objects)))
parent (position-auto-shapes parent)]
@ -1174,7 +1187,7 @@
(let [;; Temporary remove the children when moving them
frame (-> frame
(update :shapes #(d/removev children %))
(assign-cells))
(assign-cells objects))
children (->> children (remove #(position-absolute? objects %)))]
@ -1182,7 +1195,7 @@
(update :shapes d/concat-vec children)
(cond-> (some? cell)
(push-into-cell children row column))
(assign-cells))))
(assign-cells objects))))
(defn add-children-to-index
[parent ids objects to-index]

View file

@ -872,10 +872,10 @@
(pcb/update-shapes [parent-id] #(ctl/add-children-to-index % ids objects to-index)))
(pcb/update-shapes parents
(fn [parent]
(fn [parent objects]
(cond-> parent
(ctl/grid-layout? parent)
(ctl/assign-cells))))
(ctl/assign-cells objects))))
(pcb/reorder-grid-children parents)

View file

@ -70,7 +70,7 @@
update-layout-ids
(->> ids
(map (d/getf objects))
(filter #(some update-layout-attr? (pcb/changed-attrs % update-fn {:attrs attrs})))
(filter #(some update-layout-attr? (pcb/changed-attrs % objects update-fn {:attrs attrs})))
(map :id))
changes (reduce

View file

@ -182,10 +182,10 @@
(-> changes
(pcb/update-shapes
[(:parent-id first-shape)]
(fn [shape]
(fn [shape objects]
(-> shape
(ctl/push-into-cell [(:id first-shape)] row column)
(ctl/assign-cells))))
(ctl/assign-cells objects))))
(pcb/reorder-grid-children [(:parent-id first-shape)])))
changes)

View file

@ -191,14 +191,14 @@
;; Temporary remove the children when moving them
frame (-> frame
(update :shapes #(d/removev ids %))
(ctl/assign-cells))
(ctl/assign-cells objects))
ids (->> ids (remove #(ctl/position-absolute? objects %)))
frame (-> frame
(update :shapes d/concat-vec ids)
(cond-> (some? cell)
(ctl/push-into-cell ids row column))
(ctl/assign-cells))]
(ctl/assign-cells objects))]
(-> modifiers
(ctm/change-property :layout-grid-rows (:layout-grid-rows frame))

View file

@ -498,7 +498,8 @@
changes (-> (pcb/add-object changes new-obj {:ignore-touched (and duplicating-component? child?)})
(pcb/amend-last-change #(assoc % :old-id (:id obj)))
(cond-> (ctl/grid-layout? objects (:parent-id obj))
(-> (pcb/update-shapes [(:parent-id obj)] (fn [shape] (-> shape ctl/assign-cells ctl/check-deassigned-cells)))
(-> (pcb/update-shapes [(:parent-id obj)] ctl/assign-cells)
(pcb/update-shapes [(:parent-id obj)] ctl/check-deassigned-cells)
(pcb/reorder-grid-children [(:parent-id obj)]))))
changes (cond-> changes

View file

@ -79,16 +79,13 @@
(-> shape
(merge initial-layout-data)
;; (cond-> (= type :grid) (-> ctl/assign-cells ctl/reorder-grid-children))
;; If the original shape is not a frame we set clip content and show-viewer to false
(cond-> (not from-frame?)
(assoc :show-content true :hide-in-viewer true)))
params (calculate-params objects (cfh/get-immediate-children objects (:id shape)) shape)]
(cond-> (merge shape params)
(= type :grid) (-> ctl/assign-cells ctl/reorder-grid-children)))
)))
(= type :grid) (-> (ctl/assign-cells objects) ctl/reorder-grid-children))))))
;; Never call this directly but through the data-event `:layout/update`
;; Otherwise a lot of cycle dependencies could be generated
@ -277,10 +274,10 @@
(rx/of (dwu/start-undo-transaction undo-id)
(dwc/update-shapes
ids
(fn [shape]
(fn [shape objects]
(case type
:row (ctl/remove-grid-row shape index)
:column (ctl/remove-grid-column shape index))))
:row (ctl/remove-grid-row shape index objects)
:column (ctl/remove-grid-column shape index objects))))
(ptk/data-event :layout/update ids)
(dwu/commit-undo-transaction undo-id))))))
@ -435,11 +432,11 @@
(dwc/update-shapes children-ids (partial fix-child-sizing objects changes))
(dwc/update-shapes
parent-ids
(fn [parent]
(fn [parent objects]
(-> parent
(fix-parent-sizing objects (set ids) changes)
(cond-> (ctl/grid-layout? parent)
(ctl/assign-cells)))))
(ctl/assign-cells objects)))))
(ptk/data-event :layout/update ids)
(dwu/commit-undo-transaction undo-id))))))
@ -475,10 +472,9 @@
(let [undo-id (js/Symbol)]
(rx/of
(dwu/start-undo-transaction undo-id)
(dwc/update-shapes
[layout-id]
(fn [shape]
(fn [shape objects]
(case mode
:auto
;; change the manual cells and move to auto
@ -492,7 +488,7 @@
(> (:column-span cell) 1))
(-> (d/update-in-when [:layout-grid-cells cell-id] assoc :shapes [] :position :auto)
(ctl/resize-cell-area (:row cell) (:column cell) (:row cell) (:column cell) 1 1)
(ctl/assign-cells)))))
(ctl/assign-cells objects)))))
shape))
:manual
@ -503,7 +499,7 @@
(cond-> shape
(contains? #{:area :auto} (:position cell))
(-> (d/assoc-in-when [:layout-grid-cells cell-id :position] :manual)
(ctl/assign-cells)))))
(ctl/assign-cells objects)))))
shape))
:area
@ -522,7 +518,7 @@
first-column
(inc (- last-row first-row))
(inc (- last-column first-column)))
(ctl/assign-cells))]
(ctl/assign-cells objects))]
(-> shape
(d/update-in-when [:layout-grid-cells (:id target-cell)] assoc :position :area))))))
@ -535,8 +531,9 @@
(ptk/reify ::update-grid-cell-position
ptk/WatchEvent
(watch [_ _ _]
(let [undo-id (js/Symbol)]
(watch [_ state _]
(let [objects (wsh/lookup-page-objects state)
undo-id (js/Symbol)]
(rx/of
(dwu/start-undo-transaction undo-id)
(dwc/update-shapes
@ -550,6 +547,6 @@
(ctl/resize-cell-area (:row prev-data) (:column prev-data)
(:row new-data) (:column new-data)
(:row-span new-data) (:column-span new-data))
(ctl/assign-cells)))))
(ctl/assign-cells objects)))))
(ptk/data-event :layout/update [layout-id])
(dwu/commit-undo-transaction undo-id))))))

View file

@ -629,7 +629,7 @@
(-> changes
(pcb/update-shapes [(:id parent)] (fn [shape] (-> shape
(assoc :layout-grid-cells layout-grid-cells)
(ctl/assign-cells))))
(ctl/assign-cells objects))))
(pcb/reorder-grid-children [(:id parent)]))))
changes

View file

@ -196,13 +196,13 @@
{::mf/wrap-props false}
[props]
(let [shape (unchecked-get props "shape")
x (unchecked-get props "x")
y (unchecked-get props "y")
width (unchecked-get props "width")
height (unchecked-get props "height")
handler (unchecked-get props "handler")
objects (mf/deref refs/workspace-page-objects)
{cell-id :id} (unchecked-get props "cell")
{:keys [row column row-span column-span]} (get-in shape [:layout-grid-cells cell-id])
@ -237,7 +237,7 @@
shape
(-> (ctl/resize-cell-area shape row column new-row new-column new-row-span new-column-span)
(ctl/assign-cells))
(ctl/assign-cells objects))
modifiers
(-> (ctm/empty)