Skip to content

Commit

Permalink
✨ Add items to grid cells
Browse files Browse the repository at this point in the history
  • Loading branch information
Alotor committed May 6, 2024
1 parent fde0bcf commit ca7f17e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
36 changes: 26 additions & 10 deletions common/src/app/common/types/shape/layout.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,21 @@
(let [cells+index (d/enumerate cells)]
(d/seek #(in-cell? (second %) row column) cells+index)))

(defn free-cell-shapes
"Removes the shape-ids from the cells previously assigned."
[parent shape-ids]
(let [shape-ids (set shape-ids)]
(letfn [(free-cells
[cells]
(reduce-kv
(fn [m k v]
(if (some shape-ids (:shapes v))
(assoc-in m [k :shapes] [])
m))
cells
cells))]
(update parent :layout-grid-cells free-cells))))

(defn push-into-cell
"Push the shapes into the row/column cell and moves the rest"
[parent shape-ids row column]
Expand All @@ -1295,16 +1310,17 @@
;; Move shift the `shapes` attribute between cells
(->> (range start-index (inc to-index))
(map vector shape-ids)
(reduce (fn [[parent cells] [shape-id idx]]
;; If the shape to put in a cell is the same that is already in the cell we do nothing
(if (= shape-id (get-in parent [:layout-grid-cells (get-in cells [idx :id]) :shapes 0]))
[parent cells]
(let [[parent cells] (free-cell-push parent cells idx)]
[(update-in parent [:layout-grid-cells (get-in cells [idx :id])]
assoc :position :manual
:shapes [shape-id])
cells])))
[parent cells])
(reduce
(fn [[parent cells] [shape-id idx]]
;; If the shape to put in a cell is the same that is already in the cell we do nothing
(if (= shape-id (get-in parent [:layout-grid-cells (get-in cells [idx :id]) :shapes 0]))
[parent cells]
(let [[parent cells] (free-cell-push parent cells idx)]
[(update-in parent [:layout-grid-cells (get-in cells [idx :id])]
assoc :position :manual
:shapes [shape-id])
cells])))
[parent cells])
(first)))
parent)))

Expand Down
1 change: 0 additions & 1 deletion frontend/src/app/main/data/workspace/svg_upload.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
(dwu/commit-undo-transaction undo-id)))

(catch :default cause
(js/console.log (.-stack cause))
(rx/throw {:type :svg-parser
:data cause})))))))

23 changes: 16 additions & 7 deletions frontend/src/app/main/data/workspace/transforms.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@
:ignore-constraints false
:ignore-snap-pixel true}))))))

(defn- move-shapes-to-frame
(defn move-shapes-to-frame
[ids frame-id drop-index [row column :as cell]]
(ptk/reify ::move-shapes-to-frame
ptk/WatchEvent
Expand Down Expand Up @@ -923,32 +923,41 @@
changes
(-> (pcb/empty-changes it page-id)
(pcb/with-objects objects)

;; Remove layout-item properties when moving a shape outside a layout
(cond-> (not (ctl/any-layout? objects frame-id))
(pcb/update-shapes moving-shapes-ids ctl/remove-layout-item-data))

;; Remove the swap slots if it is moving to a different component
(pcb/update-shapes child-heads
(fn [shape]
(cond-> shape
(not= component-main-frame (ctn/find-component-main objects shape false))
(ctk/remove-swap-slot))))
(pcb/update-shapes
child-heads
(fn [shape]
(cond-> shape
(not= component-main-frame (ctn/find-component-main objects shape false))
(ctk/remove-swap-slot))))

;; Remove component-root property when moving a shape inside a component
(cond-> (ctn/get-instance-root objects frame)
(pcb/update-shapes moving-shapes-children-ids #(dissoc % :component-root)))

;; Add component-root property when moving a component outside a component
(cond-> (not (ctn/get-instance-root objects frame))
(pcb/update-shapes child-heads #(assoc % :component-root true)))

(pcb/update-shapes moving-shapes-ids #(cond-> % (cfh/frame-shape? %) (assoc :hide-in-viewer true)))
(pcb/update-shapes shape-ids-to-detach ctk/detach-shape)
(pcb/change-parent frame-id moving-shapes drop-index)

;; Change the grid cell in a grid layout
(cond-> (ctl/grid-layout? objects frame-id)
(-> (pcb/update-shapes
[frame-id]
(fn [frame objects]
(-> frame
;; Assign the cell when pushing into a specific grid cell
(cond-> (some? cell)
(-> (ctl/push-into-cell moving-shapes-ids row column)
(-> (ctl/free-cell-shapes moving-shapes-ids)
(ctl/push-into-cell moving-shapes-ids row column)
(ctl/assign-cells objects)))
(ctl/assign-cell-positions objects)))
{:with-objects? true})
Expand Down
1 change: 0 additions & 1 deletion frontend/src/app/main/data/workspace/zoom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@
state
(let [page-id (:current-page-id state)
objects (wsh/lookup-page-objects state page-id)
_ (prn "??" (->> ids (map #(get objects %)) (map :name)))
srect (->> ids
(map #(get objects %))
(gsh/shapes->rect))]
Expand Down
15 changes: 13 additions & 2 deletions frontend/src/app/plugins/grid.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
[app.common.record :as crc]
[app.common.spec :as us]
[app.common.types.shape.layout :as ctl]
[app.common.uuid :as uuid]
[app.main.data.workspace.shape-layout :as dwsl]
[app.main.data.workspace.transforms :as dwt]
[app.main.store :as st]
[app.plugins.utils :as utils :refer [get-data get-state]]))
[app.plugins.utils :as utils :refer [get-data get-state]]
[app.util.object :as obj]
[potok.v2.core :as ptk]))

(defn- make-tracks
[tracks]
Expand Down Expand Up @@ -72,7 +76,14 @@
(remove
[self]
(let [id (get-data self :id)]
(st/emit! (dwsl/remove-layout #{id})))))
(st/emit! (dwsl/remove-layout #{id}))))

(appendChild
[self child row column]
(let [parent-id (get-data self :id)
child-id (uuid/uuid (obj/get child "id"))]
(st/emit! (dwt/move-shapes-to-frame #{child-id} parent-id nil [row column])
(ptk/data-event :layout/update {:ids [parent-id]})))))

(defn grid-layout-proxy
[data]
Expand Down

0 comments on commit ca7f17e

Please sign in to comment.