diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 47690443de..b93304a7cf 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -461,6 +461,57 @@ (every? (fn [point] (some #(gpt/close? point %) corners)) (:points shape)) (every? #(mth/close? (get transform %) (get other-tf %)) [:a :b :c :d])))) +(defn- laid-group + "The outermost group or bool around `shape` of `objects` that a layout + places, nil when the groups around it lead to no layout." + [objects shape] + (loop [parent (get objects (:parent-id shape)) found nil] + (if (#{:group :bool} (:type parent)) + (recur (get objects (:parent-id parent)) + (if (and (ctsl/any-layout-immediate-child? objects parent) + (not (ctsl/position-absolute? parent))) + parent + found)) + found))) + +(defn- author-laid-group-arrangement + "Make authored the geometry of the copy shapes of a group a layout + places, when a transform changes one of them: the transform changes the + group's bounds and the layout moves the group back, moving every shape in + it, so the arrangement only survives sync as their own geometry. The + groups and bools take their bounds from them." + [changes before ids] + (let [objects (pcb/get-objects changes) + groups (into #{} + (keep (fn [id] + (let [shape (get objects id) + old (get before id)] + (when (and (:shape-ref shape) old + (not (gsh/close-attrs? :points (:points old) (:points shape)))) + (when-let [group (laid-group objects shape)] + ;; the arrangement changes when some of its shapes move and + ;; others stay; a transform of the whole group, or of the + ;; board that holds it, carries them all + (let [moved? (fn [id] + (not (gsh/close-attrs? :points (:points (get before id)) + (:points (get objects id))))) + shapes (remove #(#{:group :bool} (:type (get objects %))) + (cfh/get-children-ids objects (:id group)))] + (when (and (some moved? shapes) (not-every? moved? shapes)) + (:id group)))))))) + ids) + authored (into [] + (comp (mapcat #(cfh/get-children-ids objects %)) + (filter #(let [shape (get objects %)] + (and (:shape-ref shape) + (not (#{:group :bool} (:type shape))) + (not (ctk/touched-group? shape :geometry-group)))))) + groups)] + (cond-> changes + (seq authored) + (pcb/update-shapes authored #(update % :touched ctk/set-touched-group :geometry-group) + {:ignore-touched true})))) + (defn generate-sync-transformed-placement "Settle copy descendants after a reflection and untouched swapped heads after a parent turn, in the transform's change group. Reflection can @@ -497,27 +548,28 @@ reflected? [(:id root) false]))) ids))] - (reduce (fn [changes [id resize?]] - (let [objects (pcb/get-objects changes) - page (assoc page :objects objects) - file (assoc-in file [:data :pages-index (:id page) :objects] objects) - container (ctn/make-container page :page) - changes (pcb/with-container changes container) - proposal (generate-sync-shape-direct changes file (assoc libraries (:id file) file) - container id false) - placed (pcb/get-objects proposal)] - (pcb/update-shapes - changes (cfh/get-children-ids-with-self objects id) - (fn [shape] - (let [expected (get placed (:id shape)) - attrs (cond-> placement-geometry-attrs - (= :path (:type shape)) (conj :content))] - (if (and (= (:type shape) (:type expected)) - (or resize? (same-placement? shape expected))) - (merge shape (select-keys expected attrs)) - shape))) - {:ignore-touched true}))) - changes roots))) + (-> (reduce (fn [changes [id resize?]] + (let [objects (pcb/get-objects changes) + page (assoc page :objects objects) + file (assoc-in file [:data :pages-index (:id page) :objects] objects) + container (ctn/make-container page :page) + changes (pcb/with-container changes container) + proposal (generate-sync-shape-direct changes file (assoc libraries (:id file) file) + container id false) + placed (pcb/get-objects proposal)] + (pcb/update-shapes + changes (cfh/get-children-ids-with-self objects id) + (fn [shape] + (let [expected (get placed (:id shape)) + attrs (cond-> placement-geometry-attrs + (= :path (:type shape)) (conj :content))] + (if (and (= (:type shape) (:type expected)) + (or resize? (same-placement? shape expected))) + (merge shape (select-keys expected attrs)) + shape))) + {:ignore-touched true}))) + changes roots) + (author-laid-group-arrangement before ids)))) (defn- sync-copy-shape "Sync the copy shape `shape-inst` from `shape-main`, the main shape it diff --git a/common/src/app/common/test_helpers/composable/gen.cljc b/common/src/app/common/test_helpers/composable/gen.cljc index 7be4c6cace..743e8d0eea 100644 --- a/common/src/app/common/test_helpers/composable/gen.cljc +++ b/common/src/app/common/test_helpers/composable/gen.cljc @@ -387,12 +387,7 @@ 1 (and (nil? (:shape-ref shape)) (not (ctk/main-instance? shape)) (some ctk/main-instance? (cfh/get-parents objects (:id shape)))) - ;; the model places the children of a group in a - ;; copy through the board around it, and only when - ;; no layout places that group - 2 (and (:shape-ref shape) (nil? (:component-id shape)) - (or (not (#{:group :bool} (:type (get objects (:parent-id shape))))) - (not (ctl/any-layout? (get objects (:frame-id shape)))))) + 2 (and (:shape-ref shape) (nil? (:component-id shape))) 3 (and (copy-head shape) (not (top-copy shape))) 4 (top-copy shape))) (:text :text-style) (= :text (:type shape)) @@ -1131,9 +1126,44 @@ ;; a path stores no width or height: its size is that of its bounds (and (nil? (:width shape)) (not (and (mth/close? (:width (:selrect shape)) (:width (:selrect shape-after)) 1e-10) - (mth/close? (:height (:selrect shape)) (:height (:selrect shape-after)) 1e-10)))))] - (if (and (:shape-ref shape) changed?) + (mth/close? (:height (:selrect shape)) (:height (:selrect shape-after)) 1e-10))))) + ;; the outermost group or bool around the shape that a layout places + laid (loop [parent parent found nil] + (if (#{:group :bool} (:type parent)) + (recur (get objects (:parent-id parent)) + (if (and (ctl/any-layout-immediate-child? objects parent) + (not (ctl/position-absolute? parent))) + parent + found)) + found)) + after-objects (all-objects after)] + (cond + ;; moving some shapes of a group a layout places authors all its shapes + (and (:shape-ref shape) laid + ;; the group holds shapes the move leaves where they are + (let [moved (set (cfh/get-children-ids-with-self objects id))] + (some #(and (not (contains? moved %)) + (not (#{:group :bool} (:type (get objects %))))) + (cfh/get-children-ids objects (:id laid)))) + (let [modifiers (case kind + :resize (ctm/change-size shape nil (:value op)) + :rotate (n/geometry-modifiers shape :rotate (:angle op)) + (n/geometry-modifiers shape (:action op) (:value op)))] + (not (gsh/close-attrs? :points (:points shape) + (:points (gsh/transform-shape shape modifiers)) 1e-10)))) + (reduce (fn [model child-id] + (let [child (get after-objects child-id)] + (if (and (:shape-ref child) (not (#{:group :bool} (:type child))) + (or (= child-id id) (not (get-in model [child-id group])))) + (assoc-in model [child-id group] (shadow/author-geometry child nil)) + model))) + model + (cfh/get-children-ids objects (:id laid))) + + (and (:shape-ref shape) changed?) (assoc-in model [id group] (shadow/author-geometry shape-after origin (not position?))) + + :else model)) model))) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index edc37eae3d..fa7ca12fa3 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -2630,6 +2630,16 @@ :ops [[:add-child] [:structure] [:swap 5] [:add-child] [:add-child] [:structure 6 4 5] [:style 0 29 1]]})))) +(t/deftest moving-a-shape-of-a-copy-group-a-layout-places-keeps-the-arrangement + (t/is (map? (cg/run {:world [1 0 {:variants false :rich-variants false :text false :layout :flex + :layout-copies false :layout-children true}] + :ops [[:structure 0 0 19] [:transform 10 0 0 12]]})))) + +(t/deftest turning-a-whole-copy-authors-nothing-in-its-laid-out-groups + (t/is (map? (cg/run {:world [3 0 {:variants true :rich-variants false :text true :layout :grid + :layout-copies false :layout-children false}] + :ops [[:remove-child] [:structure 9] [:rotate 6]]})))) + (t/deftest settling-a-bool-in-a-grid-copy-twice-keeps-its-shapes-in-place (t/is (map? (cg/run {:world [3 0 {:variants true :rich-variants true :text true :layout :grid :layout-copies true :layout-children true}]