diff --git a/common/src/app/common/test_helpers/composable/gen.cljc b/common/src/app/common/test_helpers/composable/gen.cljc index bbb50a2233..d7970007e1 100644 --- a/common/src/app/common/test_helpers/composable/gen.cljc +++ b/common/src/app/common/test_helpers/composable/gen.cljc @@ -181,6 +181,8 @@ [["a" setup/labeled-fill] ["b" "#ff0000"]] :size 100) s) s)) + (as-> s (if (:library-variants opts) + (variants/size-container s "library-variants" 200 200) s)) (as-> s (if (and (:library-variants opts) (:rich-variants opts)) (variants/diversify s "library-variants") s)) tm/file @@ -247,6 +249,19 @@ (into {} (map (juxt (comp :id :shape) :shape)) (forest situation)) (mapcat #(vals (get-in % [:data :components])) (vals (cf/all-files situation))))) +(defn- keep-stored-entries + "The model `before` an undo, with the entries of `model` for the shapes the + undo takes off the pages of `situation` into a deleted component, which + keeps them for a restore to bring back." + [before model situation] + (let [live (into #{} (map (comp :id :shape)) (forest situation)) + stored (all-objects situation)] + (reduce-kv (fn [result id groups] + (cond-> result + (and (contains? stored id) (not (contains? live id)) (not (contains? before id))) + (assoc id groups))) + before model))) + (defn- settle-model [model situation synced?] (let [references (when (some :layout-grid-cells (vals model)) @@ -290,6 +305,7 @@ (variants/members setup/labeled-fill (:rich-variants opts) (:variant-diff opts)) :size 100)]) + s (variants/size-container s "random-variants" 200 200) s (cond-> s (:rich-variants opts) (variants/diversify "random-variants"))] (reduce #(tm/apply-to %2 %1) s [(n/make-nested-component-with-variant "variant" "random-variants" "a") @@ -1052,7 +1068,7 @@ history (:undo state) redo (:redo state) next-model (case kind - :undo (:before (peek history)) + :undo (keep-stored-entries (:before (peek history)) model after) :redo (:after (peek redo)) (update-model model situation after resolved kind)) next-model (cond-> (or next-model model) diff --git a/common/src/app/common/test_helpers/composable/variants.cljc b/common/src/app/common/test_helpers/composable/variants.cljc index d73bc42ead..ef09f9a6ec 100644 --- a/common/src/app/common/test_helpers/composable/variants.cljc +++ b/common/src/app/common/test_helpers/composable/variants.cljc @@ -68,6 +68,19 @@ :type :rect :name "Badge" :width 12 :height 12 :parent-label root-label)))) (tm/file situation) (map-indexed vector members)))))) +(defn size-container + "Size the container of set `set-name` to `width` by `height`, as the + boards the app builds around its members." + [situation set-name width height] + (let [container-id (thi/id (:container-label (n/variant-set situation set-name))) + file (tm/file situation) + page (thf/current-page file) + changes (cls/generate-update-shapes + (pcb/empty-changes nil (:id page)) [container-id] + #(gsh/transform-shape % (ctm/change-size % width height)) + (:objects page) {})] + (tm/with-file situation (thf/apply-changes file changes)))) + (defn nearest "Prefer matches in earlier unchanged properties, then container order. This predicts the contract without calling the production distance or diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 2adba1681f..1acd546750 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1864,6 +1864,21 @@ :ops [[:edit 3 0 0 1] [:variant-property 0 6 0 0] [:switch 0] [:make-variant 5] [:switch 13] [:make-variant 13 0 0 0] [:add-variant 0] [:switch 10]]})))) +(t/deftest adding-variants-beside-a-scaled-member-keeps-the-file-valid + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:delete 0] [:restore 0] [:style 1 8 1] [:add-variant 0] [:switch 0] + [:add-variant 0 0 0 0]]})))) + +(t/deftest restoring-a-variant-an-undo-removed-keeps-its-swap + (t/is (map? (cg/run {:world [3 2 {:variants true :text-styles true :layout nil :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:make-variant 10 0 0 0] [:make-variant 0] [:switch 0] [:combine 4] [:switch 12] + [:combine 15] [:switch 19] [:combine 0 0 0 0] [:make-variant 14 0 0 0] + [:undo 0 0 0 0] [:restore 0 0 0 0]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)]