diff --git a/common/src/app/common/geom/shapes.cljc b/common/src/app/common/geom/shapes.cljc index 757d8f7b34..f0776af5a9 100644 --- a/common/src/app/common/geom/shapes.cljc +++ b/common/src/app/common/geom/shapes.cljc @@ -173,6 +173,8 @@ (dm/export gtr/transform-bounds) (dm/export gtr/move-position-data) (dm/export gtr/apply-objects-modifiers) +(dm/export gtr/apply-children-modifiers) +(dm/export gtr/update-shapes-geometry) ;; Constratins (dm/export gct/calc-child-modifiers) diff --git a/common/src/app/common/geom/shapes/transforms.cljc b/common/src/app/common/geom/shapes/transforms.cljc index ade9d8a7ef..376cd41a7e 100644 --- a/common/src/app/common/geom/shapes/transforms.cljc +++ b/common/src/app/common/geom/shapes/transforms.cljc @@ -455,6 +455,29 @@ (assoc :points points)) (update-group-selrect shape children)))) +(defn update-shapes-geometry + [objects ids] + (->> ids + (reduce + (fn [objects id] + (let [shape (get objects id) + children (cph/get-immediate-children objects id) + shape + (cond + (cph/mask-shape? shape) + (update-mask-selrect shape children) + + (cph/bool-shape? shape) + (update-bool-selrect shape children objects) + + (cph/group-shape? shape) + (update-group-selrect shape children) + + :else + shape)] + (assoc objects id shape))) + objects))) + (defn transform-shape ([shape] (let [modifiers (:modifiers shape)] diff --git a/common/src/app/common/pages/changes.cljc b/common/src/app/common/pages/changes.cljc index 0b0af2fcca..a6dd6e9522 100644 --- a/common/src/app/common/pages/changes.cljc +++ b/common/src/app/common/pages/changes.cljc @@ -367,7 +367,7 @@ (comp first first)) new-shapes - (into [] (sort-by id->idx < old-shapes))] + (into [] (sort-by #(or (id->idx %) -1) < old-shapes))] (reset! changed? (not= old-shapes new-shapes)) diff --git a/common/src/app/common/types/shape/layout.cljc b/common/src/app/common/types/shape/layout.cljc index b5b5d9fdae..20617a80f1 100644 --- a/common/src/app/common/types/shape/layout.cljc +++ b/common/src/app/common/types/shape/layout.cljc @@ -528,8 +528,7 @@ :layout-justify-items :layout-grid-dir :layout-grid-columns - :layout-grid-rows - )) + :layout-grid-rows)) (defn remove-layout-item-data [shape] diff --git a/frontend/src/app/main/data/workspace/groups.cljs b/frontend/src/app/main/data/workspace/groups.cljs index 22220e2d0c..4622366297 100644 --- a/frontend/src/app/main/data/workspace/groups.cljs +++ b/frontend/src/app/main/data/workspace/groups.cljs @@ -107,12 +107,28 @@ ;; docstring to understand the real purpose of this code ids-to-delete (get-empty-groups-after-group-creation objects parent-id shapes) + target-cell + (when (ctl/grid-layout? objects parent-id) + (ctl/get-cell-by-shape-id (get objects parent-id) (-> shapes last :id))) + + grid-parents + (into [] + (comp (map :parent-id) + (filter (partial ctl/grid-layout? objects))) + shapes) + changes (-> (pcb/empty-changes it page-id) (pcb/with-objects objects) (pcb/add-object group {:index group-idx}) (pcb/update-shapes (map :id shapes) ctl/remove-layout-item-data) (pcb/change-parent (:id group) (reverse shapes)) (pcb/update-shapes (map :id shapes-to-detach) ctk/detach-shape) + (cond-> target-cell + (pcb/update-shapes + [parent-id] + (fn [parent] + (assoc-in parent [:layout-grid-cells (:id target-cell) :shapes] [(:id group)])))) + (pcb/update-shapes grid-parents ctl/assign-cells) (pcb/remove-objects ids-to-delete))] [group changes])) @@ -212,12 +228,14 @@ objects (wsh/lookup-page-objects state page-id) selected (wsh/lookup-selected state) selected (cph/clean-loops objects selected) - shapes (shapes-for-grouping objects selected)] + shapes (shapes-for-grouping objects selected) + parents (into #{} (map :parent-id) shapes)] (when-not (empty? shapes) (let [[group changes] (prepare-create-group it objects page-id shapes "Group" false)] (rx/of (dch/commit-changes changes) - (dws/select-shapes (d/ordered-set (:id group)))))))))) + (dws/select-shapes (d/ordered-set (:id group))) + (ptk/data-event :layout/update parents)))))))) (def ungroup-selected (ptk/reify ::ungroup-selected diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 06777eab70..13a6f7483a 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -299,7 +299,8 @@ (let [file-id (:current-file-id state) page-id (:current-page-id state) objects (wsh/lookup-page-objects state page-id) - shapes (dwg/shapes-for-grouping objects selected)] + shapes (dwg/shapes-for-grouping objects selected) + parents (into #{} (map :parent-id) shapes)] (when-not (empty? shapes) (let [[root _ changes] (dwlh/generate-add-component it shapes objects page-id file-id components-v2 @@ -307,7 +308,8 @@ dwsh/prepare-create-artboard-from-selection)] (when-not (empty? (:redo-changes changes)) (rx/of (dch/commit-changes changes) - (dws/select-shapes (d/ordered-set (:id root))))))))))) + (dws/select-shapes (d/ordered-set (:id root))) + (ptk/data-event :layout/update parents))))))))) (defn add-component "Add a new component to current file library, from the currently selected shapes. diff --git a/frontend/src/app/main/data/workspace/modifiers.cljs b/frontend/src/app/main/data/workspace/modifiers.cljs index 4a21d93bf4..eb3d4ea119 100644 --- a/frontend/src/app/main/data/workspace/modifiers.cljs +++ b/frontend/src/app/main/data/workspace/modifiers.cljs @@ -177,19 +177,20 @@ modif-tree))) (defn add-grid-children-modifiers - [modifiers frame-id selected objects [row column :as cell]] + [modifiers frame-id shapes objects [row column :as cell]] (let [frame (get objects frame-id) + ids (set shapes) ;; Temporary remove the children when moving them frame (-> frame - (update :shapes #(d/removev selected %)) + (update :shapes #(d/removev ids %)) (ctl/assign-cells)) - selected (->> selected (remove #(ctl/layout-absolute? objects %))) + ids (->> ids (remove #(ctl/layout-absolute? objects %))) frame (-> frame - (update :shapes d/concat-vec selected) + (update :shapes d/concat-vec ids) (cond-> (some? cell) - (ctl/push-into-cell selected row column)) + (ctl/push-into-cell ids row column)) (ctl/assign-cells))] (-> modifiers @@ -205,6 +206,7 @@ target-frame (get objects target-frame-id) target-flex-layout? (ctl/flex-layout? target-frame) + target-grid-layout? (ctl/grid-layout? target-frame) children-ids (concat (:shapes target-frame) selected) @@ -225,12 +227,12 @@ (fn [modif-tree [original-frame shapes]] (let [shapes (->> shapes (d/removev #(= target-frame-id %))) shapes (cond->> shapes - (and target-flex-layout? (= original-frame target-frame-id)) + (and (or target-grid-layout? target-flex-layout?) + (= original-frame target-frame-id)) ;; When movining inside a layout frame remove the shapes that are not immediate children (filterv #(contains? child-set %))) children-ids (->> (dm/get-in objects [original-frame :shapes]) (remove (set selected))) - h-sizing? (and (ctl/flex-layout? objects original-frame) (ctl/change-h-sizing? original-frame objects children-ids)) v-sizing? (and (ctl/flex-layout? objects original-frame) @@ -246,7 +248,11 @@ (update-in [original-frame :modifiers] ctm/change-property :layout-item-v-sizing :fix))) (and target-flex-layout? (= original-frame target-frame-id)) - (update-in [target-frame-id :modifiers] ctm/add-children shapes drop-index))))] + (update-in [target-frame-id :modifiers] ctm/add-children shapes drop-index) + + ;; Add the object to the cell + target-grid-layout? + (update-in [target-frame-id :modifiers] add-grid-children-modifiers target-frame-id shapes objects cell-data))))] (as-> modif-tree $ (reduce update-frame-modifiers $ origin-frame-ids) @@ -259,11 +265,7 @@ ;; Set fix position to target frame (vertical) (and (ctl/flex-layout? objects target-frame-id) (ctl/change-v-sizing? target-frame-id objects children-ids)) - (update-in [target-frame-id :modifiers] ctm/change-property :layout-item-v-sizing :fix) - - ;; Add the object to the cell - (ctl/grid-layout? objects target-frame-id) - (update-in [target-frame-id :modifiers] add-grid-children-modifiers target-frame-id selected objects cell-data))))) + (update-in [target-frame-id :modifiers] ctm/change-property :layout-item-v-sizing :fix))))) (defn modif->js [modif-tree objects] diff --git a/frontend/src/app/main/ui/workspace/viewport/grid_layout_editor.cljs b/frontend/src/app/main/ui/workspace/viewport/grid_layout_editor.cljs index 99ef08e8d5..8f034b48c5 100644 --- a/frontend/src/app/main/ui/workspace/viewport/grid_layout_editor.cljs +++ b/frontend/src/app/main/ui/workspace/viewport/grid_layout_editor.cljs @@ -15,6 +15,7 @@ [app.common.geom.shapes.grid-layout :as gsg] [app.common.geom.shapes.points :as gpo] [app.common.math :as mth] + [app.common.pages.helpers :as cph] [app.common.types.modifiers :as ctm] [app.common.types.shape.layout :as ctl] [app.main.data.workspace.grid-layout.editor :as dwge] @@ -734,13 +735,16 @@ children (mf/use-memo - (mf/deps shape modifiers) + (mf/deps objects shape modifiers) (fn [] - (->> (:shapes shape) - (map (d/getf objects)) - (map #(gsh/transform-shape % (dm/get-in modifiers [(:id %) :modifiers]))) - (remove :hidden) - (map #(vector (gpo/parent-coords-bounds (:points %) (:points shape)) %))))) + (let [ids (cph/get-children-ids objects (:id shape)) + objects (-> objects + (gsh/apply-objects-modifiers (select-keys modifiers ids)) + (gsh/update-shapes-geometry (reverse ids)))] + (->> (:shapes shape) + (map (d/getf objects)) + (remove :hidden) + (map #(vector (gpo/parent-coords-bounds (:points %) (:points shape)) %)))))) children (hooks/use-equal-memo children)