diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index d17580f8ef..59fab78103 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -1641,6 +1641,30 @@ (ctsl/create-cells shape [column row 1 1]))) shape added))) +(defn- assign-inherited-grid-cells + [shape before objects] + (if-not (or (< (count (:layout-grid-rows shape)) (count (:layout-grid-rows before))) + (< (count (:layout-grid-columns shape)) (count (:layout-grid-columns before)))) + (ctsl/assign-cells shape objects) + (let [active? (some #(not (ctsl/position-absolute? objects %)) (:shapes shape)) + ;; Like assign-cells, a grid with participating children needs at + ;; least one track per axis, including an initially empty grid. + target (cond-> shape + (and active? (empty? (:layout-grid-rows shape))) + (assoc :layout-grid-rows [ctsl/default-track-value]) + + (and active? (empty? (:layout-grid-columns shape))) + (assoc :layout-grid-columns [ctsl/default-track-value]))] + (-> target + (ctsl/check-deassigned-cells objects) + (ctsl/fit-cells-to-tracks) + (ctsl/assign-cells objects) + ;; New children can exhaust the remaining slots. Fit any overflow + ;; back into inherited tracks instead of deriving extra tracks. + (assoc :layout-grid-rows (:layout-grid-rows target) + :layout-grid-columns (:layout-grid-columns target)) + (ctsl/fit-cells-to-tracks))))) + (defn- update-grid-copy-attrs "Synchronizes the `layout-grid-cells` property from the main shape to the copies" [changes shape-before shape-main omit-touched?] @@ -1672,13 +1696,12 @@ copy-cells (-> shape-copy :layout-grid-cells (remove-orphan-cells shape-copy)) main-cells (-> shape-main (ctsl/remap-grid-cells ids-map) :layout-grid-cells) keep-cells? (and omit-touched? (contains? (:touched shape-copy) :layout-grid-cells))] - (-> shape-copy - (assoc :layout-grid-cells - (if keep-cells? - copy-cells - main-cells)) - (cond-> keep-cells? (add-inherited-grid-cells shape-before)) - (ctsl/assign-cells objects)))) + (let [shape-copy (assoc shape-copy :layout-grid-cells (if keep-cells? copy-cells main-cells))] + (if keep-cells? + (-> shape-copy + (add-inherited-grid-cells shape-before) + (assign-inherited-grid-cells shape-before objects)) + (ctsl/assign-cells shape-copy objects))))) {:ignore-touched true :with-objects? true}))) (defn- update-grid-main-attrs diff --git a/common/src/app/common/types/shape/layout.cljc b/common/src/app/common/types/shape/layout.cljc index c5eb7d6ccc..14fbb811dd 100644 --- a/common/src/app/common/types/shape/layout.cljc +++ b/common/src/app/common/types/shape/layout.cljc @@ -1481,6 +1481,64 @@ (contains? (set shapes) shape-id))) (second))) +(defn fit-cells-to-tracks + "Fit cell placements and spans within the current tracks. Keep occupied + cells before empty cells, authored empty areas before defaults, and keep + placements that already fit first. + If no separate slot remains, keep the receiving cell's properties and + combine their children in that cell." + [parent] + (let [rows (count (:layout-grid-rows parent)) + 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)) + (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)) + column (range column (+ column column-span))] [row column])) + fits? (fn [{:keys [row column row-span column-span]}] + (and (<= 1 row (+ row row-span -1) rows) + (<= 1 column (+ column column-span -1) cols))) + authored? (fn [cell] + (not= grid-cell-defaults + (merge grid-cell-defaults (dissoc cell :id :row :column :shapes)))) + cells (sort-by (juxt #(cond (seq (:shapes %)) 0 (authored? %) 1 :else 2) + #(if (fits? %) 0 1) :row :column (comp str :id)) + (vals (:layout-grid-cells parent))) + [cells covered] + (reduce + (fn [[cells covered :as result] cell] + (let [row (min rows (:row cell)) + column (min cols (:column cell)) + bounded (assoc cell :row row :column column + :row-span (min (:row-span cell) (inc (- rows row))) + :column-span (min (:column-span cell) (inc (- cols column)))) + free? #(and (fits? %) (not-any? covered (area %))) + places (fn [cell] (cons cell (map (fn [[row column]] (assoc cell :row row :column column)) coords))) + placed (if (or (seq (:shapes cell)) (authored? cell)) + (or (d/seek free? (places bounded)) + (d/seek free? (places (assoc bounded :row-span 1 :column-span 1)))) + (when (and (<= (:row cell) rows) (<= (:column cell) cols) (free? bounded)) bounded))] + (cond + placed + [(assoc cells (:id placed) placed) (into covered (area placed))] + + (seq (:shapes cell)) + (let [target (d/seek #(in-cell? % row column) (vals cells))] + [(update cells (:id target) + #(cond-> (update % :shapes d/concat-vec (:shapes cell)) + (= :auto (:position %)) (assoc :position :manual))) + covered]) + + :else result))) + [{} #{}] cells)] + (reduce (fn [parent [row column :as coord]] + (if (contains? covered coord) + parent + (create-cells parent [column row 1 1]))) + (assoc parent :layout-grid-cells cells) coords))))) + (defn swap-shapes [parent id-from id-to] diff --git a/common/test/common_tests/logic/comp_sync_test.cljc b/common/test/common_tests/logic/comp_sync_test.cljc index 36cf9e5a21..f59550cdb6 100644 --- a/common/test/common_tests/logic/comp_sync_test.cljc +++ b/common/test/common_tests/logic/comp_sync_test.cljc @@ -31,10 +31,79 @@ [app.common.types.file :as ctf] [app.common.types.modifiers :as ctm] [app.common.types.shape-tree :as ctst] + [app.common.types.shape.layout :as ctl] [clojure.test :as t])) (t/use-fixtures :each thi/test-fixture) +(t/deftest removed-main-grid-tracks-keep-overridden-copy-cells-in-bounds + (doseq [axis [:row :column] + span? [false true]] + (let [initial (cg/world [1 0 {:layout :grid :layout-children true :grid-square true}]) + main-id ((n/main-head-of "random-0") initial) + copy-id ((n/nth-copy-head-of "random-0" 0) initial) + other-id ((n/nth-copy-head-of "random-0" 1) initial) + index (if span? 1 0) + coord (if (= (= axis :row) span?) [1 2] [2 1]) + original (tm/shape-by-id initial copy-id) + authored (cond-> original + span? (ctl/resize-cell-area (first coord) (second coord) + (first coord) (second coord) + (if (= axis :row) 2 1) (if (= axis :column) 2 1))) + cell-id (:id (ctl/cell-by-row-column authored (first coord) (second coord))) + authored (update-in authored [:layout-grid-cells cell-id] assoc :align-self :end :justify-self :center) + original-file (tm/file initial) + original-page (thf/current-page original-file) + override (cls/generate-update-shapes + (pcb/empty-changes nil (:id original-page)) #{copy-id} + #(assoc % :layout-grid-cells (:layout-grid-cells authored)) + (:objects original-page) {}) + file (thf/apply-changes original-file override) + page (thf/current-page file) + main (get-in page [:objects main-id]) + copy (get-in page [:objects copy-id]) + cell (first (filter #(= coord [(:row %) (:column %)]) (vals (:layout-grid-cells copy)))) + child-id (first (:shapes cell)) + ids (set ((if (= axis :row) ctl/shapes-by-row ctl/shapes-by-column) main index)) + [_ deletion] (cls/generate-delete-shapes (pcb/empty-changes) (:data file) page (:objects page) ids {}) + deleted (thf/apply-changes file deletion) + objects (:objects (thf/current-page deleted)) + removal (cls/generate-update-shapes + (pcb/empty-changes nil (:id page)) #{main-id} + (fn [shape objects] + ((if (= axis :row) ctl/remove-grid-row ctl/remove-grid-column) shape index objects)) + objects {:with-objects? true}) + removed (thf/apply-changes deleted removal) + synced (r/sync-cascade removed {} #{(:component-id main)}) + after (:file synced) + changes (reduce pcb/concat-changes (pcb/concat-changes deletion removal) (:syncs synced)) + undone (thf/apply-undo-changes after changes) + redone (thf/apply-changes undone changes) + get-shape #(ths/get-shape-by-id after %) + retained (first (filter #(some #{child-id} (:shapes %)) (vals (:layout-grid-cells (get-shape copy-id))))) + tracks (if (= axis :row) :layout-grid-rows :layout-grid-columns)] + (t/is (some? child-id)) + (t/is (= 1 (count (get (get-shape main-id) tracks)))) + (t/is (= (get (get-shape main-id) tracks) (get (get-shape copy-id) tracks))) + (doseq [id [main-id copy-id other-id] + :let [shape (get-shape id) + rows (count (:layout-grid-rows shape)) + cols (count (:layout-grid-columns shape))]] + (t/is (every? #(and (<= 1 (:row %) (+ (:row %) (:row-span %) -1) rows) + (<= 1 (:column %) (+ (:column %) (:column-span %) -1) cols)) + (vals (:layout-grid-cells shape))) + (pr-str {:axis axis :id id :rows rows :columns cols}))) + (t/is (= :end (:align-self retained))) + (t/is (= :center (:justify-self retained))) + (t/is (= cell-id (:id retained))) + (t/is (= (if span? coord [1 1]) [(:row retained) (:column retained)])) + (t/is (= [1 1] [(:row-span retained) (:column-span retained)])) + (t/is (= #{:layout-grid-cells} (:touched (get-shape copy-id)))) + (t/is (uc/same-state? (uc/snapshot file) (uc/snapshot undone))) + (t/is (uc/same-state? (uc/snapshot after) (uc/snapshot redone))) + (t/is (uc/same-state? (uc/snapshot after) + (uc/snapshot (:file (r/sync-cascade after {} #{(:component-id main)})))))))) + (t/deftest grid-growth-fills-inherited-tracks-before-assigning-copy-children (doseq [direction [:row :column] turn-copy? [false true]] diff --git a/common/test/common_tests/types/shape_layout_test.cljc b/common/test/common_tests/types/shape_layout_test.cljc index 4ec0ef268b..53e5a325f7 100644 --- a/common/test/common_tests/types/shape_layout_test.cljc +++ b/common/test/common_tests/types/shape_layout_test.cljc @@ -50,6 +50,93 @@ ;; Layout predicates ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(t/deftest fitting-grid-cells-keeps-occupied-placement-and-metadata + (let [a (make-cell :row 1 :column 1 :shapes [(uuid/next)] :align-self :start) + b (make-cell :row 3 :column 1 :row-span 2 :shapes [(uuid/next)] + :position :area :area-name "Kept area" :align-self :end :justify-self :center) + empty (make-cell :row 1 :column 2) + shape (make-grid-frame :layout-grid-rows [layout/default-track-value] + :layout-grid-columns [layout/default-track-value layout/default-track-value] + :layout-grid-cells {(:id a) a (:id b) b (:id empty) empty}) + after (layout/fit-cells-to-tracks shape) + kept (get-in after [:layout-grid-cells (:id b)])] + (t/is (= a (get-in after [:layout-grid-cells (:id a)]))) + (t/is (= (assoc b :row 1 :column 2 :row-span 1) kept)) + (t/is (= #{(:id a) (:id b)} (set (keys (:layout-grid-cells after))))) + (t/is (empty? (layout/overlapping-cells after))) + (t/is (= (:layout-grid-rows shape) (:layout-grid-rows after))) + (t/is (= (:layout-grid-columns shape) (:layout-grid-columns after))) + (t/is (= after (layout/fit-cells-to-tracks after))))) + +(t/deftest fitting-grid-cells-keeps-an-empty-authored-area + (let [area (make-cell :row 2 :column 1 :position :area :area-name "Kept empty area" :align-self :end) + a (make-cell :row 1 :column 1) + b (make-cell :row 1 :column 2) + shape (make-grid-frame :layout-grid-rows [layout/default-track-value] + :layout-grid-columns [layout/default-track-value layout/default-track-value] + :layout-grid-cells {(:id area) area (:id a) a (:id b) b}) + after (layout/fit-cells-to-tracks shape)] + (t/is (= (assoc area :row 1) (get-in after [:layout-grid-cells (:id area)]))) + (t/is (= 2 (count (:layout-grid-cells after)))) + (t/is (empty? (layout/overlapping-cells after))) + (t/is (= after (layout/fit-cells-to-tracks after))))) + +(t/deftest fitting-grid-cells-shortens-spans-around-retained-cells + (let [a (make-cell :row 1 :column 1 :row-span 2 :shapes [(uuid/next)] :position :manual) + b (make-cell :row 3 :column 1 :row-span 2 :column-span 2 :shapes [(uuid/next)] + :position :manual :align-self :end) + shape (make-grid-frame :layout-grid-rows [layout/default-track-value layout/default-track-value] + :layout-grid-columns [layout/default-track-value layout/default-track-value] + :layout-grid-cells {(:id a) a (:id b) b}) + after (layout/fit-cells-to-tracks shape)] + (t/is (= a (get-in after [:layout-grid-cells (:id a)]))) + (t/is (= (assoc b :row 1 :column 2 :row-span 1 :column-span 1) + (get-in after [:layout-grid-cells (:id b)]))) + (t/is (= #{[1 1] [1 2] [2 2]} + (set (map (juxt :row :column) (vals (:layout-grid-cells after)))))) + (t/is (empty? (layout/overlapping-cells after))) + (t/is (= after (layout/fit-cells-to-tracks after))))) + +(t/deftest fitting-grid-cells-keeps-all-children-when-slots-run-out + (let [children [(make-shape) (make-shape)] + ids (mapv :id children) + a (make-cell :row 1 :column 1 :shapes [(first ids)] :align-self :end) + b (make-cell :row 2 :column 1 :shapes [(second ids)] :position :manual :align-self :end) + shape (make-grid-frame :layout-grid-rows [layout/default-track-value] + :layout-grid-columns [layout/default-track-value] + :shapes ids :layout-grid-cells {(:id a) a (:id b) b}) + after (layout/fit-cells-to-tracks shape)] + (t/is (= {(:id a) (assoc a :shapes ids :position :manual)} (:layout-grid-cells after))) + (t/is (= ids (vec (mapcat :shapes (vals (:layout-grid-cells after)))))) + (t/is (= after (layout/assign-cells after (into {} (map (juxt :id identity)) children)))) + (t/is (= after (layout/fit-cells-to-tracks after))))) + +(t/deftest fitting-grid-cells-uses-the-existing-flow-order + (doseq [direction [:row :column nil]] + (let [a (make-cell :row 1 :column 1 :shapes [(uuid/next)] :position :manual) + b (make-cell :row 2 :column 2 :shapes [(uuid/next)] :position :manual) + moved (make-cell :row 3 :column 3 :shapes [(uuid/next)] :position :manual) + shape (make-grid-frame :layout-grid-rows [layout/default-track-value layout/default-track-value] + :layout-grid-columns [layout/default-track-value layout/default-track-value] + :layout-grid-cells {(:id a) a (:id b) b (:id moved) moved}) + 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 (empty? (layout/overlapping-cells after))) + (t/is (= after (layout/fit-cells-to-tracks after)))))) + +(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) + shape (make-grid-frame :layout-grid-rows (vec (repeat rows layout/default-track-value)) + :layout-grid-columns (vec (repeat columns layout/default-track-value)) + :layout-grid-cells {(:id cell) cell}) + after (layout/fit-cells-to-tracks shape)] + (t/is (= {} (:layout-grid-cells after))) + (t/is (= (:layout-grid-rows shape) (:layout-grid-rows after))) + (t/is (= (:layout-grid-columns shape) (:layout-grid-columns after)))))) + (t/deftest flex-layout?-test (t/testing "returns true for flex frame" (t/is (layout/flex-layout? (make-flex-frame)))) diff --git a/frontend/test/frontend_tests/composable_tests/completion_contract_test.cljs b/frontend/test/frontend_tests/composable_tests/completion_contract_test.cljs index e4f55b3684..1e608f4643 100644 --- a/frontend/test/frontend_tests/composable_tests/completion_contract_test.cljs +++ b/frontend/test/frontend_tests/composable_tests/completion_contract_test.cljs @@ -54,6 +54,93 @@ (t/use-fixtures :each ftm/fixture) +(t/deftest ^:async removing-a-main-grid-track-keeps-overridden-copy-cells-in-bounds + (await + (mock/with-mocks* {rp/cmd! mock/rpc-cmd-mock rx/timer mock/timer-mock} + (let [initial (cg/world [1 0 {:layout :grid :layout-children true :grid-square true}]) + main-id ((n/main-head-of "random-0") initial) + copy-id ((n/nth-copy-head-of "random-0" 0) initial) + other-id ((n/nth-copy-head-of "random-0" 1) initial) + get-shape #(ths/get-shape-by-id (ftm/current-file) %) + get-cell (fn [shape id] (first (filter #(some #{id} (:shapes %)) + (vals (:layout-grid-cells shape))))) + copy (tm/shape-by-id initial copy-id) + cell (first (filter #(= [2 1] [(:row %) (:column %)]) + (vals (:layout-grid-cells copy)))) + child-id (first (:shapes cell)) + _ (ftm/install! initial)] + (t/is (some? child-id)) + (t/is (= :settled (await (ftm/await-step [(ptk/data-event :layout/update {:ids [main-id copy-id other-id]})])))) + (let [before (ftm/snapshot)] + (t/is (= :settled (await (ftm/await-step [(dwsl/update-grid-cells copy-id [(:id cell)] {:align-self :end})])))) + (let [overridden (ftm/snapshot)] + (t/is (= :settled (await (ftm/await-step [dwu/undo])))) + (t/is (uc/same-state? before (ftm/snapshot))) + (t/is (= :settled (await (ftm/await-step [dwu/redo])))) + (t/is (uc/same-state? overridden (ftm/snapshot))) + (t/is (= :settled (await (ftm/await-step [(dwsl/remove-layout-track [main-id] :row 0 :with-shapes? true)])))) + (let [removed (ftm/snapshot)] + (t/is (= 1 (count (:layout-grid-rows (get-shape main-id))))) + (doseq [id [main-id copy-id other-id] + :let [shape (get-shape id) + rows (count (:layout-grid-rows shape)) + cols (count (:layout-grid-columns shape))]] + (t/is (every? #(and (<= 1 (:row %) (+ (:row %) (:row-span %) -1) rows) + (<= 1 (:column %) (+ (:column %) (:column-span %) -1) cols)) + (vals (:layout-grid-cells shape))) + (pr-str {:id id :rows rows :columns cols + :cells (mapv #(select-keys % [:row :column :row-span :column-span]) + (vals (:layout-grid-cells shape)))}))) + (t/is (= :end (:align-self (get-cell (get-shape copy-id) child-id)))) + (t/is (= #{:layout-grid-cells} (:touched (get-shape copy-id)))) + (t/is (= :settled (await (ftm/await-step [dwu/undo])))) + (t/is (uc/same-state? overridden (ftm/snapshot))) + (t/is (= :settled (await (ftm/await-step [dwu/redo])))) + (t/is (uc/same-state? removed (ftm/snapshot)))))) + (ftm/uninstall!))))) + +(t/deftest ^:async removed-grid-tracks-shorten-spans-but-keep-authored-track-lists + (await + (mock/with-mocks* {rp/cmd! mock/rpc-cmd-mock rx/timer mock/timer-mock} + (doseq [axis [:row :column] own-tracks? [false true]] + (let [initial (cg/world [1 0 {:layout :grid :layout-children true :grid-square true}]) + main-id ((n/main-head-of "random-0") initial) + copy-id ((n/nth-copy-head-of "random-0" 0) initial) + coord (if (= axis :row) [1 2] [2 1]) + track (if (= axis :row) :layout-grid-rows :layout-grid-columns) + span (if (= axis :row) :row-span :column-span) + copy (tm/shape-by-id initial copy-id) + cell (first (filter #(= coord [(:row %) (:column %)]) (vals (:layout-grid-cells copy)))) + child-id (first (:shapes cell)) + _ (ftm/install! initial)] + (t/is (= :settled (await (ftm/await-step [(ptk/data-event :layout/update {:ids [main-id copy-id]})])))) + (when own-tracks? + (t/is (= :settled (await (ftm/await-step [(dwsl/change-layout-track [copy-id] axis 0 {:value 137})]))))) + (t/is (= :settled (await (ftm/await-step [(dwsl/update-grid-cell-position copy-id (:id cell) {span 2})])))) + (t/is (= :settled (await (ftm/await-step [(dwsl/update-grid-cells copy-id [(:id cell)] + {:align-self :end :justify-self :center})])))) + (let [before (ftm/snapshot) + old-copy (ths/get-shape-by-id (ftm/current-file) copy-id)] + (t/is (= :settled (await (ftm/await-step [(dwsl/remove-layout-track [main-id] axis 1 :with-shapes? true)])))) + (let [after (ftm/snapshot) + copy (ths/get-shape-by-id (ftm/current-file) copy-id) + main (ths/get-shape-by-id (ftm/current-file) main-id) + retained (first (filter #(some #{child-id} (:shapes %)) (vals (:layout-grid-cells copy))))] + (t/is (= 1 (count (get main track)))) + (t/is (= (if own-tracks? (get old-copy track) (get main track)) (get copy track))) + (t/is (= (if own-tracks? 2 1) (get retained span))) + (t/is (= coord [(:row retained) (:column retained)])) + (t/is (= (:id cell) (:id retained))) + (t/is (= [:end :center] [(:align-self retained) (:justify-self retained)])) + (t/is (= (cond-> #{:layout-grid-cells} own-tracks? (conj track)) (:touched copy))) + (t/is (= :settled (await (ftm/await-step [dwu/undo])))) + (t/is (uc/same-state? before (ftm/snapshot))) + (t/is (= :settled (await (ftm/await-step [dwu/redo])))) + (t/is (uc/same-state? after (ftm/snapshot))) + (t/is (= :settled (await (ftm/await-step [(dwl/sync-file (:id (ftm/current-file)) (:id (ftm/current-file)))])))) + (t/is (uc/same-state? after (ftm/snapshot))))) + (ftm/uninstall!)))))) + (t/deftest ^:async making-a-grid-copy-child-absolute-keeps-parent-cells-derived (await (mock/with-mocks* {rp/cmd! mock/rpc-cmd-mock rx/timer mock/timer-mock}