From 6f185c624ff30f220bb27e9d3801026b79325417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Mon, 26 Oct 2026 12:25:00 +0100 Subject: [PATCH] :bug: Fix swap when the original main component is not available (#12333) * :bug: Fix swap when the original main component is not available * :bug: Change the shape-ref for the id --- common/src/app/common/types/file.cljc | 5 +- .../logic/swap_missing_library_test.cljc | 79 +++++++++++++++++++ common/test/common_tests/runner.cljc | 2 + 3 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 common/test/common_tests/logic/swap_missing_library_test.cljc diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index df4733c25a..56ac4a747d 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -534,12 +534,13 @@ shape-meta (meta ref-shape) ref-file (:file shape-meta) ref-container (:container shape-meta)] - (when ref-shape + (if ref-shape (if-let [swap-slot (ctk/get-swap-slot ref-shape)] swap-slot (if (ctk/main-instance? ref-shape) (:id shape) - (find-swap-slot ref-shape ref-container ref-file libraries (conj viewed-ids (:id shape))))))))))) + (find-swap-slot ref-shape ref-container ref-file libraries (conj viewed-ids (:id shape))))) + (:id shape))))))) ;; If cannot locate ref-shape (e.g. in an unlinked external library), use what we have (defn match-swap-slot? [shape-main shape-inst container-inst container-main file libraries] diff --git a/common/test/common_tests/logic/swap_missing_library_test.cljc b/common/test/common_tests/logic/swap_missing_library_test.cljc new file mode 100644 index 0000000000..64853a1a74 --- /dev/null +++ b/common/test/common_tests/logic/swap_missing_library_test.cljc @@ -0,0 +1,79 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns common-tests.logic.swap-missing-library-test + (:require + [app.common.files.validate :as cfv] + [app.common.test-helpers.components :as thc] + [app.common.test-helpers.compositions :as tho] + [app.common.test-helpers.files :as thf] + [app.common.test-helpers.ids-map :as thi] + [app.common.test-helpers.shapes :as ths] + [clojure.test :as t])) + +(t/use-fixtures :each thi/test-fixture) + +(defn- setup-library [] + ;; {:frame-a} [:name Frame1] # [Component :component-a] + ;; :child-a [:name Rect1] + (-> (thf/sample-file :library) + (tho/add-simple-component :component-a :frame-a :child-a + :root-params {:name "component-a"} + :child-params {:name "child-a" + :type :rect + :fills (ths/sample-fills-color :fill-color "#111111")}))) + +(defn- setup-file [library] + ;; {:frame-b} [:name Frame1] # [Component :component-b] + ;; :copy-a [:name component-a] @--> [Component :component-a] (in library) :frame-a + ;; [:name child-a] ---> :child-a + ;; + ;; :copy-b [:name Frame1] #--> [Component :component-b] :frame-b + ;; [:name component-a] @--> [Component :component-a] (in library) :copy-a + ;; [:name child-a] ---> + ;; + ;; {:frame-c} [:name Frame1] # [Component :component-c] + ;; :child-c [:name Rect1] + (-> (thf/sample-file :file) + (tho/add-frame :frame-b) + (thc/instantiate-component :component-a :copy-a + :parent-label :frame-b + :library library) + (thc/make-component :component-b :frame-b) + (thc/instantiate-component :component-b :copy-b) + (tho/add-simple-component :component-c :frame-c :child-c + :root-params {:name "component-c"} + :child-params {:name "child-c" + :type :rect + :fills (ths/sample-fills-color :fill-color "#333333")}))) + +(defn- propagate-all-component-changes [file] + (-> file + (tho/propagate-component-changes :component-c) + (tho/propagate-component-changes :component-b))) + +(defn- fill-colors [file] + [(tho/bottom-fill-color file :frame-b) + (tho/bottom-fill-color file :copy-b)]) + +(t/deftest test-swap-nested-copy-with-main-in-missing-library + (let [library (setup-library) + file (setup-file library) + + ;; The library is not passed to the swap, so the main of A is not + ;; available. + file' (tho/swap-component-in-first-child file :copy-b :component-c)] + + (cfv/validate-file! file' {(:id library) library}) + + (t/testing "after the swap, B and its copy contain the new copy of C" + (t/is (= ["#111111" "#333333"] (fill-colors file')))) + + (t/testing "changes in the main of C propagate to B and its copy" + (let [file'' (tho/update-bottom-color file' :frame-c "#444444" + :propagate-fn propagate-all-component-changes)] + + (t/is (= ["#111111" "#444444"] (fill-colors file''))))))) diff --git a/common/test/common_tests/runner.cljc b/common/test/common_tests/runner.cljc index e78fbf3da9..884c9a7356 100644 --- a/common/test/common_tests/runner.cljc +++ b/common/test/common_tests/runner.cljc @@ -72,6 +72,7 @@ [common-tests.logic.swap-and-reset-test] [common-tests.logic.swap-as-override-test] [common-tests.logic.swap-keeps-id-test] + [common-tests.logic.swap-missing-library-test] [common-tests.logic.text-sync-test] [common-tests.logic.text-touched-test] [common-tests.logic.token-apply-test] @@ -178,6 +179,7 @@ 'common-tests.logic.swap-and-reset-test 'common-tests.logic.swap-as-override-test 'common-tests.logic.swap-keeps-id-test + 'common-tests.logic.swap-missing-library-test 'common-tests.logic.text-sync-test 'common-tests.logic.text-touched-test 'common-tests.logic.token-apply-test