diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 461ddec047..95c89c7b64 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -391,6 +391,45 @@ #(update % :touched (fnil conj #{}) :geometry-group) {:ignore-touched true})))) +(defn- grid-cells-by-path + "The cells of the grid `shape` without their ids, with each child as its + path (see `add-unique-path`)." + [shape path-of] + (into #{} + (map (fn [cell] (-> cell (dissoc :id) (update :shapes #(mapv path-of %))))) + (vals (:layout-grid-cells shape)))) + +(defn- carry-grid-cells + "Carry the cell overrides of the switched grid `old` into `new`, its clean + copy of the new variant, when the old main `old-ref` and `new` have + identical grids: the same tracks and the same cells holding + corresponding children. Otherwise the new variant's cells win. + `counterpart` gives the child of `new` that relates to a child of `old`." + [changes old old-ref new old-path new-path counterpart] + (let [identical? (and (= (:layout-grid-rows old-ref) (:layout-grid-rows new)) + (= (:layout-grid-columns old-ref) (:layout-grid-columns new)) + (= (grid-cells-by-path old-ref old-path) + (grid-cells-by-path new new-path))) + cell-id (fn [{:keys [row column]}] + (some (fn [[id cell]] (when (and (= row (:row cell)) (= column (:column cell))) id)) + (:layout-grid-cells new))) + cells (when identical? + (reduce (fn [cells cell] + (let [shapes (mapv counterpart (:shapes cell)) + id (or (cell-id cell) (:id cell))] + (if (some nil? shapes) + (reduced nil) + (assoc cells id (assoc cell :id id :shapes shapes))))) + {} + (vals (:layout-grid-cells old))))] + (if cells + (pcb/update-shapes changes [(:id new)] + #(-> % + (assoc :layout-grid-cells cells) + (update :touched (fnil conj #{}) :layout-grid-cells)) + {:ignore-touched true}) + changes))) + (defn generate-keep-touched "This is used as part of the switch process, when you switch from an original-shape to a new-shape. It generate changes to @@ -466,6 +505,25 @@ (clsw/update-attrs-on-switch changes new old new-shape original-shape old-ref container))) changes pairs) + + ;; Cell overrides carry only between identical grids. + new-paths (into {} (map (juxt :id :shape-path)) new-shapes-w-path) + counterpart (fn [id] + (when-let [old-child (get page-objects id)] + (let [old-ref (find-shape-ref-child-of container libraries old-child + (:id orig-base-ref-shape))] + (:id (get new-shapes-map (get o-ref-shapes-p-map (:id old-ref))))))) + grid-pairs (filter (fn [{:keys [old new swapped?]}] + (and (not swapped?) + (ctsl/grid-layout? old) + (ctsl/grid-layout? new) + (ctk/touched-group? old :layout-grid-cells))) + pairs) + changes (reduce (fn [changes {:keys [old old-ref new]}] + (carry-grid-cells changes old old-ref new + o-ref-shapes-p-map new-paths counterpart)) + changes + grid-pairs) root-geometry? (some #(and (= (:id %) (:id original-shape)) (ctk/touched-group? % :geometry-group)) orig-touched) parent (get page-objects (:parent-id original-shape)) @@ -486,7 +544,7 @@ nil libraries (assoc container :objects (pcb/get-objects changes)) (:id new-shape) false)) changes (clsw/generate-sync-swap-constraints changes page libraries (:id new-shape))] - [changes (mapv (comp :parent-id :new) pairs)])) + [changes (into (mapv (comp :parent-id :new) pairs) (map (comp :id :new)) grid-pairs)])) (defn- shape-index "Position of `shape-id` among the children of `parent-id`; 0 when it is not diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index 78b47306f4..1537260392 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -6,6 +6,7 @@ (ns common-tests.logic.variants-switch-test (:require + [app.common.data :as d] [app.common.files.changes-builder :as pcb] [app.common.files.helpers :as cfh] [app.common.geom.point :as gpt] @@ -26,6 +27,7 @@ [app.common.types.component :as ctk] [app.common.types.modifiers :as ctm] [app.common.types.path.segment :as segment] + [app.common.types.shape.layout :as ctl] [app.common.types.text :as txt] [clojure.set :as set] [clojure.string :as string] @@ -3748,3 +3750,90 @@ (t/is (= #{:geometry-group} (:touched text))) (t/is (= #{:geometry-group} (:touched (copy-child file' :copy02)))))) + +(defn- add-grid-member + "Add to the variant `variant-label` the member `component-label`: a grid + board `root-label` with two rows and `columns` columns, holding Item 1, + Item 2 and Item 3 at the `[row column]` of `cells`." + [file variant-label component-label root-label value columns cells] + (let [variant-id (thi/id variant-label) + labels (mapv #(keyword (str (name root-label) "-item" %)) [1 2 3]) + file (reduce (fn [file [index label]] + (ths/add-sample-shape file label :type :rect :name (str "Item " index) + :parent-label root-label)) + (ths/add-sample-shape file root-label :type :frame :parent-label variant-label + :variant-id variant-id :variant-name value + :layout :grid :layout-grid-dir :row + :layout-grid-rows [] :layout-grid-columns [] + :layout-grid-cells {}) + (map vector [1 2 3] labels)) + frame (reduce (fn [frame _] (ctl/add-grid-column frame {:type :flex :value 1})) + (-> (ths/get-shape file root-label) + (ctl/add-grid-row {:type :flex :value 1}) + (ctl/add-grid-row {:type :flex :value 1})) + (range columns)) + cells (reduce (fn [cells [label [row column]]] + (let [[id _] (d/seek #(and (= row (:row (val %))) (= column (:column (val %)))) cells)] + (update cells id assoc :shapes [(thi/id label)] :position :manual))) + (:layout-grid-cells frame) + (map vector labels cells))] + (-> file + (ths/update-shape root-label :layout-grid-rows (:layout-grid-rows frame)) + (ths/update-shape root-label :layout-grid-columns (:layout-grid-columns frame)) + (ths/update-shape root-label :layout-grid-cells cells) + (thc/make-component component-label root-label) + (thc/update-component component-label {:variant-id variant-id + :variant-properties [{:name "Style" :value value}]})))) + +(defn- item-cell + "The `[row column]` of the child named `item-name` of the grid `shape`." + [file shape item-name] + (let [objects (:objects (thf/current-page file)) + item (d/seek #(= item-name (:name (get objects %))) (:shapes shape)) + cell (ctl/get-cell-by-shape-id shape item)] + [(:row cell) (:column cell)])) + +(defn- grid-variants-with-moved-copy + "Blue and Green are identical 2x2 grids; Wide has three columns and puts + Item 3 in the first row. The copy of Blue moves Item 3 to row 2, + column 2, a cell override." + [] + (let [file (-> (thf/sample-file :file1) + (ths/add-sample-shape :v01 :type :frame :is-variant-container true) + (add-grid-member :v01 :blue :blue-root "Blue" 2 [[1 1] [1 2] [2 1]]) + (add-grid-member :v01 :green :green-root "Green" 2 [[1 1] [1 2] [2 1]]) + (add-grid-member :v01 :wide :wide-root "Wide" 3 [[1 1] [1 2] [1 3]]) + (thc/instantiate-component :blue :copy01)) + page (thf/current-page file) + copy (ths/get-shape file :copy01) + item3 (d/seek #(= "Item 3" (:name (get (:objects page) %))) (:shapes copy)) + moved (fn [cells] + (-> cells + (update-vals #(update % :shapes (fn [shapes] (filterv (partial not= item3) shapes)))) + (as-> cells + (let [[id _] (d/seek #(and (= 2 (:row (val %))) (= 2 (:column (val %)))) cells)] + (update cells id assoc :shapes [item3] :position :manual))))) + changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) #{(:id copy)} + #(update % :layout-grid-cells moved) + (:objects page) {})] + (thf/apply-changes file changes))) + +(t/deftest a-switch-between-identical-grids-keeps-the-cell-override + (let [file (grid-variants-with-moved-copy) + copy (ths/get-shape file :copy01) + file' (tho/swap-component-in-shape file :copy01 :green {:new-shape-label :copy02 :keep-touched? true}) + copy' (ths/get-shape file' :copy02)] + (t/is (= [2 2] (item-cell file copy "Item 3"))) + (t/is (ctk/touched-group? copy :layout-grid-cells)) + (t/is (= [2 2] (item-cell file' copy' "Item 3"))) + (t/is (= [1 1] (item-cell file' copy' "Item 1"))) + (t/is (= [1 2] (item-cell file' copy' "Item 2"))) + (t/is (ctk/touched-group? copy' :layout-grid-cells)))) + +(t/deftest a-switch-to-a-different-grid-takes-the-new-variants-cells + (let [file (grid-variants-with-moved-copy) + file' (tho/swap-component-in-shape file :copy01 :wide {:new-shape-label :copy02 :keep-touched? true}) + copy' (ths/get-shape file' :copy02)] + (t/is (= [1 3] (item-cell file' copy' "Item 3"))) + (t/is (= [1 1] (item-cell file' copy' "Item 1"))) + (t/is (not (ctk/touched-group? copy' :layout-grid-cells)))))