diff --git a/common/src/app/common/types/shape/layout.cljc b/common/src/app/common/types/shape/layout.cljc index 14fbb811dd..36fd9d8aa9 100644 --- a/common/src/app/common/types/shape/layout.cljc +++ b/common/src/app/common/types/shape/layout.cljc @@ -197,6 +197,12 @@ (and (cfh/frame-shape? shape) (= :grid (:layout shape))))) +(defn grid-direction + "The direction a grid flows its auto-placed cells in. A grid without one + flows by rows, the direction grids are created with." + [shape] + (d/nilv (:layout-grid-dir shape) :row)) + (defn any-layout? ([objects id] (any-layout? (get objects id))) @@ -1054,9 +1060,9 @@ (reorder-grid-track parent from-index to-index move-content? :layout-grid-rows :row)) (defn cells-seq - [{:keys [layout-grid-cells layout-grid-dir]} & {:keys [sort?] :or {sort? false}}] + [{:keys [layout-grid-cells] :as parent} & {:keys [sort?] :or {sort? false}}] - (let [comp-fn (if (= layout-grid-dir :row) + (let [comp-fn (if (= (grid-direction parent) :row) (juxt :row :column) (juxt :column :row)) maybe-sort? @@ -1242,11 +1248,11 @@ free-cells (get-free-cells parent) to-add-tracks - (if (= (:layout-grid-dir parent) :row) + (if (= (grid-direction parent) :row) (mth/ceil (/ (- (count no-cell-shapes) (count free-cells)) (count (:layout-grid-rows parent)))) (mth/ceil (/ (- (count no-cell-shapes) (count free-cells)) (count (:layout-grid-columns parent))))) - add-track (if (= (:layout-grid-dir parent) :row) add-grid-column add-grid-row) + add-track (if (= (grid-direction parent) :row) add-grid-column add-grid-row) parent (->> (range to-add-tracks) @@ -1492,7 +1498,7 @@ cols (count (:layout-grid-columns parent))] (if (or (zero? rows) (zero? cols)) (assoc parent :layout-grid-cells {}) - (let [coords (vec (sort-by (if (= :row (:layout-grid-dir parent)) identity (juxt second first)) + (let [coords (vec (sort-by (if (= :row (grid-direction parent)) identity (juxt second first)) (for [row (range 1 (inc rows)) column (range 1 (inc cols))] [row column]))) area (fn [{:keys [row column row-span column-span]}] (for [row (range row (+ row row-span)) diff --git a/common/test/common_tests/types/shape_layout_test.cljc b/common/test/common_tests/types/shape_layout_test.cljc index 53e5a325f7..4b989694f9 100644 --- a/common/test/common_tests/types/shape_layout_test.cljc +++ b/common/test/common_tests/types/shape_layout_test.cljc @@ -6,6 +6,7 @@ (ns common-tests.types.shape-layout-test (:require + [app.common.files.builder :as fb] [app.common.types.shape :as cts] [app.common.types.shape.layout :as layout] [app.common.uuid :as uuid] @@ -122,10 +123,59 @@ shape (if direction (assoc shape :layout-grid-dir direction) (dissoc shape :layout-grid-dir)) after (layout/fit-cells-to-tracks shape) cell (get-in after [:layout-grid-cells (:id moved)])] - (t/is (= (if (= :row direction) [1 2] [2 1]) [(:row cell) (:column cell)])) + (t/is (= (if (= :column direction) [2 1] [1 2]) [(:row cell) (:column cell)])) (t/is (empty? (layout/overlapping-cells after))) (t/is (= after (layout/fit-cells-to-tracks after)))))) +(defn- build-grid-with-children + "A grid board with `tracks` rows and columns and three children without + cells, built as library imports build files. Returns the board after cell + assignment and the children's ids." + [direction tracks] + (let [board-id (uuid/next) + child-ids [(uuid/next) (uuid/next) (uuid/next)] + state (-> (fb/create-state) + (fb/add-file {:name "Grid file"}) + (fb/add-page {:name "Page 1"}) + (fb/add-board (cond-> {:id board-id :name "Grid" :x 0 :y 0 :width 200 :height 200 + :layout :grid + :layout-grid-rows [] + :layout-grid-columns [] + :layout-grid-cells {}} + (some? direction) (assoc :layout-grid-dir direction)))) + state (reduce (fn [state id] + (fb/add-shape state {:id id :type :rect :name "Rect" :x 0 :y 0 :width 10 :height 10})) + state + child-ids) + objects (fb/get-current-objects state) + board (reduce (fn [board _] + (-> board + (layout/add-grid-row layout/default-track-value) + (layout/add-grid-column layout/default-track-value))) + (get objects board-id) + (range tracks))] + [(layout/assign-cells board objects) child-ids])) + +(defn- child-coords + [board child-ids] + (mapv #(let [cell (layout/get-cell-by-shape-id board %)] [(:row cell) (:column cell)]) child-ids)) + +(t/deftest a-grid-without-direction-assigns-children-like-a-row-grid + (doseq [tracks [0 2]] + (let [[absent absent-ids] (build-grid-with-children nil tracks) + [row row-ids] (build-grid-with-children :row tracks)] + (t/is (not (contains? absent :layout-grid-dir))) + (t/is (= (child-coords row row-ids) (child-coords absent absent-ids)))))) + +(t/deftest a-grid-without-direction-reflows-to-column-like-a-row-grid + ;; The direction event reads a missing direction as :row. + (let [[absent absent-ids] (build-grid-with-children nil 2) + [row row-ids] (build-grid-with-children :row 2) + absent (layout/reflow-grid-auto-items-for-direction absent :row :column) + row (layout/reflow-grid-auto-items-for-direction row :row :column)] + (t/is (= [[1 1] [2 1] [1 2]] (child-coords row row-ids))) + (t/is (= (child-coords row row-ids) (child-coords absent absent-ids))))) + (t/deftest fitting-a-grid-with-no-tracks-clears-empty-cells (doseq [rows [0 1] columns [0 1] :when (or (zero? rows) (zero? columns))] (let [cell (make-cell :row 1 :column 1) diff --git a/frontend/src/app/main/data/workspace/shape_layout.cljs b/frontend/src/app/main/data/workspace/shape_layout.cljs index 2fef3a0165..b66493e085 100644 --- a/frontend/src/app/main/data/workspace/shape_layout.cljs +++ b/frontend/src/app/main/data/workspace/shape_layout.cljs @@ -302,7 +302,7 @@ (dwsh/update-shapes ids (fn [shape] - (let [from-dir (d/nilv (:layout-grid-dir shape) :row)] + (let [from-dir (ctl/grid-direction shape)] (cond (not= :grid (:layout shape)) shape diff --git a/frontend/test/frontend_tests/composable_tests/grid_layout_test.cljs b/frontend/test/frontend_tests/composable_tests/grid_layout_test.cljs index f8f13023fa..6ad8352668 100644 --- a/frontend/test/frontend_tests/composable_tests/grid_layout_test.cljs +++ b/frontend/test/frontend_tests/composable_tests/grid_layout_test.cljs @@ -22,6 +22,70 @@ (t/use-fixtures :each ftm/fixture) +(defn- get-grid-file + [layout] + (let [cell (fn [row column label] + (let [id (uuid/next)] + [id {:id id :row row :column column :row-span 1 :column-span 1 + :position :auto :align-self :auto :justify-self :auto + :shapes [(thi/id label)]}]))] + (-> (thf/sample-file :file) + (tho/add-frame :main (merge layout + {:width 240 :height 160 + :layout-grid-rows [{:type :fixed :value 80} {:type :fixed :value 80}] + :layout-grid-columns [{:type :fixed :value 120} {:type :fixed :value 120}]})) + (tho/add-rect :a :parent-label :main :width 40 :height 20) + (tho/add-rect :b :parent-label :main :width 40 :height 20) + (tho/add-rect :c :parent-label :main :width 40 :height 20) + (tho/add-rect :d :parent-label :main :width 40 :height 20) + (ths/update-shape :main :layout-grid-cells + (into {} [(cell 1 1 :a) (cell 1 2 :b) (cell 2 1 :c) (cell 2 2 :d)])) + (thc/make-component :component :main) + (thc/instantiate-component :component :copy) + (thc/instantiate-component :component :other)))) + +(defn- ^:async install-grid + ([] + (await (install-grid cls/initial-grid-layout))) + ([layout] + (ftm/install! (tm/make-situation (get-grid-file layout))) + (t/is (= :settled (await (ftm/await-step + [(ptk/data-event :layout/update {:ids [(thi/id :main) (thi/id :copy) (thi/id :other)]})])))) + (t/is (= (get-cells :main) (get-cells :copy) (get-cells :other))))) + +(t/deftest ^:async a-grid-main-without-direction-grows-and-turns-like-a-row-grid + (await + (mock/with-mocks* {rp/cmd! mock/rpc-cmd-mock rx/timer mock/timer-mock} + (await (install-grid (dissoc cls/initial-grid-layout :layout-grid-dir))) + (t/is (not (contains? (get-shape :main) :layout-grid-dir))) + (let [initial (tm/make-situation (ftm/current-file)) + _ (ftm/uninstall!) + grown (loop [live (ftm/start-generated initial) + ops [(n/add-child :main :extra-a :width 20 :height 20) + (n/add-child :main :extra-b :width 20 :height 20)]] + (if-let [op (first ops)] + (let [next (await (ftm/run-generated-step live op))] + (t/is (some? next)) + (t/is (empty? (uc/failures next))) + (recur next (rest ops))) + live)) + members (set (:shapes (get-shape :main)))] + (doseq [label [:main :copy :other]] + (t/is (= [2 3] (mapv #(count (get (get-shape label) %)) [:layout-grid-rows :layout-grid-columns]))) + (t/is (= #{[1 1] [1 2] [1 3] [2 1] [2 2] [2 3]} (set (map (juxt :row :column) (get-cells label))))) + (t/is (= members (set (mapcat :shapes (get-cells label)))))) + (t/is (empty? (:touched (get-shape :copy)))) + (t/is (empty? (:touched (get-shape :other)))) + (t/is (empty? (uc/failures (await (ftm/finish-generated (ftm/refresh-generated grown))))))) + (let [rows (get-cells :main)] + (await (apply-step (dwsl/change-grid-direction [(thi/id :main)] :column))) + (t/is (= [:column :column :column] (mapv #(:layout-grid-dir (get-shape %)) [:main :copy :other]))) + (t/is (= (get-cells :main) (get-cells :copy) (get-cells :other))) + (t/is (not= rows (get-cells :main))) + (t/is (empty? (:touched (get-shape :copy)))) + (t/is (empty? (:touched (get-shape :other))))) + (ftm/uninstall!)))) + (t/deftest ^:async grid-growth-keeps-overrides-within-inherited-tracks (await (mock/with-mocks* {rp/cmd! mock/rpc-cmd-mock rx/timer mock/timer-mock}