diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 7b582ee02a..c513a5dbcc 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -671,14 +671,36 @@ (pcb/concat-changes changes placement)) changes))) +(defn update-main-nesting-loop? + "Whether updating the main of the copy head `shape-id` of `container` from + the copy would nest its component inside itself: the copy holds a copy of + its component, or of a component whose main holds one." + [libraries container shape-id] + (let [component-id (:component-id (ctn/get-shape container shape-id)) + holds? (fn [{:keys [component-file] :as head}] + (let [library (dm/get-in libraries [component-file :data]) + component (ctkl/get-component library (:component-id head) true) + root (when component (ctf/get-component-root library component))] + (and root + (some #(= component-id (:component-id %)) + (cfh/get-children (:objects (ctf/get-component-container library component)) + (:id root))))))] + (boolean + (some (fn [shape] + (and (:component-id shape) + (or (= component-id (:component-id shape)) (holds? shape)))) + (cfh/get-children (:objects container) shape-id))))) + (defn generate-sync-shape-inverse "Generate changes to update the component a shape is linked to, from - the values in the shape and all its children." + the values in the shape and all its children. An update that would nest + the component inside itself changes nothing." [changes libraries container shape-id] (shape-log :debug shape-id container :msg "Sync shape inverse" :shape (str shape-id)) (let [shape-inst (ctn/get-shape container shape-id) library (dm/get-in libraries [(:component-file shape-inst) :data]) - component (ctkl/get-component library (:component-id shape-inst)) + component (when-not (update-main-nesting-loop? libraries container shape-id) + (ctkl/get-component library (:component-id shape-inst))) changes (cond-> changes (and component (contains? (:touched shape-inst) :name-group)) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index dc25f2c33e..5948466ad0 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1,6 +1,7 @@ (ns common-tests.logic.composable-gen-test (:require [app.common.files.changes-builder :as pcb] + [app.common.files.helpers :as cfh] [app.common.geom.point :as gpt] [app.common.geom.shapes :as gsh] [app.common.logic.shapes :as cls] @@ -21,6 +22,7 @@ [app.common.test-helpers.ids-map :as thi] [app.common.test-helpers.shapes :as ths] [app.common.test-helpers.texts :as thtx] + [app.common.types.component :as ctk] [app.common.uuid :as uuid] [clojure.test :as t] [clojure.walk :as walk])) @@ -477,6 +479,26 @@ (t/is (= 1 (get-in result [:coverage :random :swap :changed]))) (t/is (= 1 (get-in result [:coverage :random :library-sync :changed]))))) +(t/deftest update-main-does-not-nest-a-component-inside-itself + ;; A swap in a variant's main reaches a slot switched to that variant inside + ;; a copy of the swap's new component, so the copy holds its own component. + (let [program {:world [2 2 {:variants true :rich-variants true :text true :text-styles true + :library true :layout :flex :layout-copies true}] + :ops [[:nest 9 0 0 0] [:switch 9 0 0 0] [:nest 2 0 0 0] [:delete 0 0 0 0] + [:swap 10 0 0 0] [:edit 3 0 1 1] [:update-main 3]]} + before (:situation (cg/settle-state (reduce cg/step (cg/initial-state (:world program)) + (butlast (:ops program))))) + objects (:objects (thf/current-page (tm/file before))) + nests-itself? (fn [shape] + (some #(= (:component-id shape) (:component-id %)) + (cfh/get-children objects (:id shape)))) + copy (some #(when (and (ctk/instance-root? %) (not (ctk/main-instance? %)) (nests-itself? %)) %) + (vals objects)) + updated (-> before (r/step (n/update-main (constantly (:id copy)))) r/settle)] + (t/is (some? copy)) + (t/is (= (oracle/snapshot before) (oracle/snapshot updated))) + (t/is (map? (cg/run program))))) + (t/deftest pushing-a-local-override-does-not-accept-unrelated-source-edits (let [spec [2 1 {:library true :stale-libraries true :library-variants true}] initial (cg/initial-state spec)