diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 59fab78103..91074ceeef 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -503,12 +503,24 @@ (gsh/close-attrs? :points (:points shape) (:points other)) (mth/close? (:content-scale shape 1) (:content-scale other 1)))) +(defn- authored-constraints + "The constraints that the copy shapes `ids` author, by the id of the main + shape each one pairs with." + [objects ids] + (into {} + (keep (fn [id] + (let [shape (get objects id)] + (when (ctk/touched-group? shape :constraints-group) + [(:shape-ref shape) (d/without-nils (select-keys shape [:constraints-h :constraints-v]))])))) + ids)) + (defn- placed-like "The main `objects` with the subtree of `shape-main` placed like `placed`, a copy shape expressed in the main's frame: resized to its size, its children following through their constraints and layouts, then turned - and moved with it as one piece." - [objects shape-main placed] + and moved with it as one piece. A child resizes through the constraints + its copy authors (`authored`, by main shape id) instead of the main's." + [objects shape-main placed authored] (let [scale (/ (:content-scale placed 1) (:content-scale shape-main 1)) width (-> placed :selrect :width) height (-> placed :selrect :height) @@ -517,7 +529,7 @@ objects (let [modif-tree (gm/set-objects-modifiers {(:id shape-main) {:modifiers (ctm/change-size shape-main width height)}} - objects)] + (reduce-kv #(d/update-when %1 %2 merge %3) objects authored))] (reduce-kv (fn [objects id {:keys [modifiers]}] (d/update-when objects id gsh/transform-shape modifiers)) objects @@ -586,7 +598,9 @@ (in-main-frame shape-inst (:root-inst ctx) (:root-main ctx))) main-container (cond-> main-container (and (some? placed) (not (close-geometry? placed shape-main))) - (update :objects placed-like shape-main placed)) + (update :objects placed-like shape-main placed + (authored-constraints (:objects container) + (cfh/get-children-ids (:objects container) (:id shape-inst))))) ctx (assoc ctx :main-container main-container) shape-main (get-in main-container [:objects (:id shape-main)]) changes (-> changes @@ -1575,7 +1589,7 @@ main-objects (:objects (:container (meta main))) main-parent (get main-objects (:parent-id main))] (if (and parent main-parent) - (let [placed (get (placed-like main-objects main-parent parent) (:id main))] + (let [placed (get (placed-like main-objects main-parent parent (authored-constraints objects [id])) (:id main))] (generate-take-placement changes shape placed objects (and (ctsl/any-layout-immediate-child? objects shape) (not (ctsl/position-absolute? shape))))) diff --git a/common/test/common_tests/logic/comp_expected_geometry_test.cljc b/common/test/common_tests/logic/comp_expected_geometry_test.cljc index 2da8cbf014..c43e19e30e 100644 --- a/common/test/common_tests/logic/comp_expected_geometry_test.cljc +++ b/common/test/common_tests/logic/comp_expected_geometry_test.cljc @@ -288,6 +288,27 @@ (t/is (= box (child-box synced)) "the sync keeps it there") (t/is (= "#ff0000" (-> (ths/get-shape synced :copy-child) :fills first :fill-color))))))) +(t/deftest a-resized-copy-root-places-children-through-their-authored-constraints + (doseq [[constraints-h box] [[:right [170 10 20 20]] + [:scale [140 10 40 20]] + [:leftright [70 10 120 20]]]] + (t/testing (name constraints-h) + (let [file (component-with-child :left) + page (thf/current-page file) + authored (thf/apply-changes file + (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) + #{(thi/id :copy-child)} + #(assoc % :constraints-h constraints-h) + (:objects page) + {})) + resized (resize authored :copy-root 200 100) + synced (edit-and-sync resized)] + (t/is (= #{:constraints-group} (:touched (ths/get-shape resized :copy-child)))) + (t/is (= box (child-box resized)) "the resize places the child") + (t/is (= box (child-box synced)) "the sync keeps it there") + (t/is (= constraints-h (:constraints-h (ths/get-shape synced :copy-child)))) + (t/is (= "#ff0000" (-> (ths/get-shape synced :copy-child) :fills first :fill-color))))))) + (t/deftest a-copy-root-resized-with-its-content-touches-only-the-root (let [file (-> (component-with-child :scale) (ths/update-shape :main-child :strokes [{:stroke-color "#000000" :stroke-opacity 1 diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index e864c546b2..227e4e33ef 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -12,6 +12,7 @@ [app.common.test-helpers.composable.comp.setups :as setup] [app.common.test-helpers.composable.core :as tm] [app.common.test-helpers.composable.gen :as cg] + [app.common.test-helpers.composable.grid :as grid] [app.common.test-helpers.composable.oracle :as oracle] [app.common.test-helpers.composable.shadow :as shadow] [app.common.test-helpers.composable.variants :as variants] @@ -19,7 +20,9 @@ [app.common.test-helpers.ids-map :as thi] [app.common.test-helpers.shapes :as ths] [app.common.test-helpers.texts :as thtx] - [clojure.test :as t])) + [app.common.uuid :as uuid] + [clojure.test :as t] + [clojure.walk :as walk])) (t/use-fixtures :each thi/test-fixture) @@ -645,6 +648,20 @@ [:undo] [:redo] [:reset 0] [:sync 1] [:switch 1 0 0 0] [:detach 0 0 0 0] [:delete 0 0 0 0]]})))) +(t/deftest resizing-a-copy-scales-a-child-through-its-authored-constraint + (let [program {:world [2 1 {:text true :text-styles true :layout nil}] + :ops [[:delete 0 0 0 0] [:style 5 9 1] [:resize 1 0 0 0]]} + situation (:situation (cg/run program)) + file (tm/file situation) + page (thf/current-page file) + child (:shape (first (filter #(= [2 0 0] (:path %)) (cg/tree situation)))) + expected (oracle/expected-geometry file page (cf/libraries situation) child)] + (t/is (= :scale (:constraints-v child))) + (t/is (= #{:constraints-group} (:touched child))) + ;; The parent shrinks from 100 to 80, so a scaled 50 child becomes 40. + (t/is (gsh/close-attrs? :height 40 (:height expected) 0.01)) + (t/is (gsh/close-attrs? :height 40 (:height child) 0.01)))) + (t/deftest detached-swapped-head-follows-its-own-component-without-position-overrides (t/is (map? (cg/run {:world [2 1 {:library true :variants true}] :ops [[:edit 0 0 1 2] [:update-main 0] [:library-sync]