diff --git a/common/src/app/common/files/changes_builder.cljc b/common/src/app/common/files/changes_builder.cljc index 94f50fccef..bf14116302 100644 --- a/common/src/app/common/files/changes_builder.cljc +++ b/common/src/app/common/files/changes_builder.cljc @@ -523,7 +523,10 @@ mk-undo-change (fn [undo-changes shape] - (let [prev-sibling (cfh/get-prev-sibling objects (:id shape))] + ;; Use the shape as it is in the objects, as the sibling lookup + ;; does: the given map may predate earlier moves in these changes + (let [shape (get objects (:id shape) shape) + prev-sibling (cfh/get-prev-sibling objects (:id shape))] (conj undo-changes (cond-> {:type :mov-objects :page-id (::page-id (meta changes)) diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 68eaad19e6..8a33212c50 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -2995,11 +2995,13 @@ [changes objects shape file page libraries id-new-component index target-cell keep-props-values ignore-swapped?] (let [;; When we keep the touched properties, we can't delete the - ;; swapped children (we will keep them too) + ;; swapped children (we will keep them too). That includes the + ;; children swapped in an outer main, above the main of the shape ignore-swapped-fn (if ignore-swapped? - #(-> (get objects %) - (ctk/get-swap-slot)) + (let [outer-swaps (ctf/get-outer-swaps (ctn/make-container page :page) libraries shape)] + #(or (some? (ctk/get-swap-slot (get objects %))) + (contains? outer-swaps %))) (constantly false)) ;; In the cases where the swapped shape was the first element of the masked group it would make the group to loose the diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 4804067aa8..154b328b35 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -110,6 +110,33 @@ [[] {}] shapes)))) +(defn- move-to-root-first + "Adds, before all the `changes` of a switch, a move of `shape` to the root, + so it is not deleted with the original copy (its undo, run last, puts it + back once the original copy is restored)." + [changes page shape] + (-> (pcb/empty-changes) + (pcb/with-page page) + (pcb/with-objects (:objects page)) + (pcb/change-parent uuid/zero [shape] 0 {:allow-altering-copies true}) + (pcb/concat-changes changes) + ;; Keep the other keys of the switch changes, as :origin or :undo-group + (merge (dissoc changes :redo-changes :undo-changes)))) + +(defn- drop-swapped-item + "As part of the keep-touched process on a switch, delete a child on the + original copy that was swapped (orig-swapped-child) and has no related + shape on the new copy." + [changes orig-swapped-child ldata page] + (let [changes (move-to-root-first changes page orig-swapped-child)] + ;; These are the page objects with the child moved to the root. The + ;; delete replays all the changes on them, so the working objects end + ;; as the ones after the switch, for the next steps. + (-> (cls/generate-delete-shapes changes ldata page (pcb/get-objects changes) + (d/ordered-set (:id orig-swapped-child)) + {:allow-altering-copies true}) + second))) + (defn- keep-swapped-item "As part of the keep-touched process on a switch, given a child on the original copy that was swapped (orig-swapped-child), and its related shape on the new copy @@ -120,15 +147,7 @@ orig-swapped-child follows its parent through its constraints and, if the user didn't move it, takes the place of related-shape-in-new." [changes related-shape-in-new orig-swapped-child ldata page swap-ref-id] - (let [;; Before to the swap, temporary move the previous - ;; shape to the root panel to avoid problems when - ;; the previous parent is deleted. - before-changes (-> (pcb/empty-changes) - (pcb/with-page page) - (pcb/with-objects (:objects page)) - (pcb/change-parent uuid/zero [orig-swapped-child] 0 {:allow-altering-copies true})) - - objects (pcb/get-objects changes) + (let [objects (pcb/get-objects changes) page-objects (:objects page) prev-swap-slot (ctk/get-swap-slot orig-swapped-child) current-parent (get objects (:parent-id related-shape-in-new)) @@ -140,7 +159,7 @@ #(gpt/subtract (grc/rect->point (:selrect related-shape-in-new)) (grc/rect->point (:selrect %))))] - (-> (pcb/concat-changes before-changes changes) + (-> (move-to-root-first changes page orig-swapped-child) ;; Move the previous shape to the new parent (pcb/change-parent (:parent-id related-shape-in-new) [orig-swapped-child] pos {:allow-altering-copies true}) @@ -167,8 +186,9 @@ second))) (defn- child-of-swapped? - "Check if any ancestor of a shape (between base-parent-id and shape) was swapped" - [shape objects base-parent-id] + "Check if any ancestor of a shape (between base-parent-id and shape) was swapped, + in the copy itself or in an outer main (`outer-swaps`, by head id)" + [shape objects base-parent-id outer-swaps] (let [ancestors (->> (ctn/get-parent-heads objects shape) ;; Ignore ancestors ahead of base-parent (drop-while #(not= base-parent-id (:id %))) @@ -177,7 +197,29 @@ ;; Ignore first and last (base-parent and shape) ancestors (when (and ancestors (<= 3 num-ancestors)) (subvec (vec ancestors) 1 (dec num-ancestors)))] - (some ctk/get-swap-slot ancestors))) + (some #(or (ctk/get-swap-slot %) (contains? outer-swaps (:id %))) ancestors))) + +(defn- repoint-outer-swapped + "A head swapped in an outer main (`orig-head`, `level` levels up its ref chain) + has been moved into the new copy, in the place of `related-shape-in-new`. + Make it and its children point to the swapped component instead of the + outer main, and give the head the slot of `related-shape-in-new`." + [changes page libraries orig-head level related-shape-in-new] + (let [refs (ctf/get-advanced-refs nil (ctn/make-container page :page) libraries + (:id orig-head) level orig-head)] + ;; The working objects of `changes` lost the head when the original copy + ;; was deleted, so take the shapes from the page + (->> (-> (pcb/empty-changes) + (pcb/with-page page) + (pcb/with-objects (:objects page)) + (pcb/update-shapes (keys refs) + (fn [shape] + (let [{:keys [shape-ref touched]} (get refs (:id shape))] + (cond-> (assoc shape :shape-ref shape-ref :touched touched) + (= (:id shape) (:id orig-head)) + (ctk/set-swap-slot (:shape-ref related-shape-in-new))))) + {:ignore-touched true})) + (pcb/concat-changes changes)))) (defn- find-shape-ref-child-of "Get the shape referenced by the shape-ref of the near main of the shape, @@ -218,18 +260,29 @@ container (ctn/make-container page :page) page-objects (:objects page) - ;; Get the touched children of the original-shape + ;; The original-shape is in a copy. For the relation rules, we need the referenced + ;; shape on the main component + orig-base-ref-shape (ctf/find-remote-shape container libraries original-shape {:with-context? true}) + orig-ref-objects (:objects (:container (meta orig-base-ref-shape))) + + ;; Heads swapped in an outer main (when the original-shape is nested + ;; in another copy), with no swap of their own, by id: [level slot] + outer-swaps (ctf/get-outer-swaps container libraries original-shape) + + ;; Get the touched children of the original-shape, and the ones + ;; swapped in an outer main. ;; Ignore children of swapped items, because ;; they will be moved without change when ;; managing their swapped ancestor orig-touched (->> original-shapes ;; Add to each shape also the touched of its ref chain (map #(add-touched-from-ref-chain container libraries %)) - (filter (comp seq :touched)) + (filter #(or (seq (:touched %)) (contains? outer-swaps (:id %)))) (remove #(child-of-swapped? % page-objects - (:id original-shape)))) + (:id original-shape) + outer-swaps))) ;; Adds a :shape-path attribute to the children of the new-shape, ;; that contains the type of its ancestors and its name @@ -240,11 +293,6 @@ ;; Creates a map to quickly find a child of the new-shape by its shape-path new-shapes-map (into {} (map (juxt :shape-path identity)) new-shapes-w-path) - ;; The original-shape is in a copy. For the relation rules, we need the referenced - ;; shape on the main component - orig-base-ref-shape (ctf/find-remote-shape container libraries original-shape {:with-context? true}) - orig-ref-objects (:objects (:container (meta orig-base-ref-shape))) - ;; Adds a :shape-path attribute to the children of the orig-ref-shape, ;; that contains the type of its ancestors and its name o-ref-shapes-wp (add-unique-path @@ -259,8 +307,10 @@ [changes parents-of-swapped] (reduce (fn [[changes parent-of-swapped] orig-child-touched] - (let [;; If the orig-child-touched was swapped, get its swap-slot - swap-slot (ctk/get-swap-slot orig-child-touched) + (let [;; If the orig-child-touched was swapped, get its swap-slot. + ;; If it was swapped in an outer main, outer-swap is [level slot] + outer-swap (get outer-swaps (:id orig-child-touched)) + swap-slot (or (ctk/get-swap-slot orig-child-touched) (second outer-swap)) ;; orig-child-touched is in a copy. Get the referenced shape on the main component ;; If there is a swap slot, we will get the referenced shape in another way @@ -268,8 +318,14 @@ (find-shape-ref-child-of container libraries orig-child-touched (:id orig-base-ref-shape))) orig-ref-id (if swap-slot - ;; If there is a swap slot, find the referenced shape id - (ctf/find-ref-id-for-swapped orig-child-touched container libraries) + ;; If there is a swap slot, find the referenced shape id, + ;; looking from the match of its parent on the main component + (let [parent (get page-objects (:parent-id orig-child-touched)) + parent-head (ctn/get-head-shape page-objects parent) + parent-ref (find-shape-ref-child-of container libraries parent-head + (:id orig-base-ref-shape))] + (when parent-ref + (ctf/find-next-related-swap-shape-id parent-ref swap-slot libraries))) ;; If there is not a swap slot, get the id from the orig-ref-shape (:id orig-ref-shape)) @@ -285,16 +341,30 @@ parent-of-swapped) ;; If there is a related shape, keep its data changes - (if related-shape-in-new + (cond + (and swap-slot + (nil? related-shape-in-new) + (not= (:id orig-child-touched) (:id original-shape))) + ;; The swapped child has no place on the new copy + (drop-swapped-item changes orig-child-touched ldata page) + + related-shape-in-new (if swap-slot ;; If the orig-child-touched was swapped, keep it - (keep-swapped-item changes related-shape-in-new orig-child-touched - ldata page orig-ref-id) + (cond-> (keep-swapped-item changes related-shape-in-new orig-child-touched + ldata page orig-ref-id) + ;; If it was swapped in an outer main, point it to the swapped component + (some? outer-swap) + (repoint-outer-swapped page libraries + (get page-objects (:id orig-child-touched)) + (first outer-swap) related-shape-in-new)) ;; If the orig-child-touched wasn't swapped, copy ;; the touched attributes into it (cll/update-attrs-on-switch changes related-shape-in-new orig-child-touched new-shape original-shape orig-ref-shape container)) + + :else changes)] [changes parents-of-swapped])) [changes []] diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 43111c2c09..490981fb13 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -550,7 +550,7 @@ (or (= slot-main slot-inst) (= (:id shape-main) slot-inst))))) -(defn- find-next-related-swap-shape-id +(defn find-next-related-swap-shape-id "Go up from the chain of references shapes that will eventually lead to the shape with swap-slot-id as id. Returns the next shape on the chain" [parent swap-slot-id libraries] @@ -1311,6 +1311,25 @@ (recur (inc level) ref (:container (meta ref))) max-levels)))) +(defn find-outer-swap + "Returns [level slot] for the first shape in the ref chain of `shape`, + excluding itself, that has been swapped there (level 1 is the ref shape). + Returns nil if the chain ends, or reaches a shape under `base-id` + first, so swaps made inside `base-id` are not returned." + [container libraries shape base-id] + (loop [level 1 + current shape + container container] + (when-let [ref (find-ref-shape nil container libraries current + :include-deleted? true + :with-context? true)] + (let [ref-container (:container (meta ref))] + (when-not (->> (cfh/get-parents-with-self (:objects ref-container) (:id ref)) + (some #(= base-id (:id %)))) + (if-let [slot (ctk/get-swap-slot ref)] + [level slot] + (recur (inc level) ref ref-container))))))) + (defn get-touched-from-ref-chain-until-target-ref "Returns a set with the :touched of all the items on the shape ref chain until target-ref, including itself" @@ -1320,4 +1339,47 @@ :always (map :touched) :always (reduce set/union #{}) remove-slots? (remove ctk/swap-slot?))] - (set/union (or (:touched shape) #{}) (set more-touched)))) \ No newline at end of file + (set/union (or (:touched shape) #{}) (set more-touched)))) + +(defn get-outer-swaps + "Map from id to [level slot] (see `find-outer-swap`) of the nested heads + inside the copy `head`, with no swap slot of their own, that were swapped + in an outer main, above the main of `head`. This happens when `head` is + nested in another copy." + [container libraries head] + (let [base-id (:id (find-remote-shape container libraries head))] + (into {} + (keep (fn [shape] + (when (and (ctk/instance-head? shape) + (nil? (ctk/get-swap-slot shape))) + (when-let [outer-swap (find-outer-swap container libraries shape base-id)] + [(:id shape) outer-swap])))) + (cfh/get-children (:objects container) (:id head))))) + +(defn get-advanced-refs + "Map from id to the new :shape-ref and :touched of `shape` and its children, + advanced by `level-delta` levels in the ref chain. A nested head swapped in + one of the levels dropped advances only up to there, as the swap already + points it to the swapped-in main. The swap slots of the chain are removed + only for the head `head-id`. A shape whose ref does not change, or whose + chain cannot be resolved (e.g. a missing library), is left out." + [file container libraries head-id level-delta shape] + (let [level-delta (if (ctk/instance-head? shape) + (get-swap-level container libraries shape level-delta) + level-delta)] + (if (pos? level-delta) + (let [new-shape-ref (advance-shape-ref file container libraries shape level-delta + :include-deleted? true) + refs (if (and (some? new-shape-ref) + (not= new-shape-ref (:shape-ref shape))) + {(:id shape) + {:shape-ref new-shape-ref + :touched (get-touched-from-ref-chain-until-target-ref + container libraries shape new-shape-ref + :remove-slots? (= (:id shape) head-id))}} + {})] + (reduce #(merge %1 (get-advanced-refs file container libraries head-id level-delta + (get (:objects container) %2))) + refs + (:shapes shape))) + {}))) \ No newline at end of file diff --git a/common/test/common_tests/files/changes_builder_test.cljc b/common/test/common_tests/files/changes_builder_test.cljc new file mode 100644 index 0000000000..99ebd6c5f8 --- /dev/null +++ b/common/test/common_tests/files/changes_builder_test.cljc @@ -0,0 +1,46 @@ +;; 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.files.changes-builder-test + (:require + [app.common.files.changes-builder :as pcb] + [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) + +(t/deftest test-change-parent-undo-uses-parent-in-objects + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (tho/add-frame :parent1) + (tho/add-frame :parent2) + (tho/add-frame :parent3) + (tho/add-rect :rect0 :parent-label :parent1) + (tho/add-rect :rect1 :parent-label :parent1) + (tho/add-rect :rect2 :parent-label :parent1)) + page (thf/current-page file) + ;; The map of the shape as it is before both moves + rect1 (ths/get-shape file :rect1) + + ;; ==== Action + changes (-> (pcb/empty-changes nil (:id page)) + (pcb/with-objects (:objects page)) + (pcb/change-parent (thi/id :parent2) [rect1]) + (pcb/change-parent (thi/id :parent3) [rect1])) + file' (thf/apply-changes file changes) + file'' (thf/apply-undo-changes file' changes)] + + (t/is (= (thi/id :parent3) (:parent-id (ths/get-shape file' :rect1)))) + ;; The undo of the second move, run first, takes the shape back to + ;; :parent2, where the first move left it, not to the parent in the map + (t/is (= (thi/id :parent2) (:parent-id (first (:undo-changes changes))))) + ;; The whole undo puts the shape back in its place in :parent1 + (t/is (= (thi/id :parent1) (:parent-id (ths/get-shape file'' :rect1)))) + (t/is (= (:shapes (ths/get-shape file :parent1)) + (:shapes (ths/get-shape file'' :parent1)))))) diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index 4410b0a887..b77e06d17a 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -12,6 +12,7 @@ [app.common.geom.shapes :as gsh] [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] + [app.common.logic.variants :as clv] [app.common.math :as mth] [app.common.test-helpers.components :as thc] [app.common.test-helpers.compositions :as tho] @@ -21,6 +22,7 @@ [app.common.test-helpers.variants :as thv] [app.common.types.component :as ctk] [app.common.types.modifiers :as ctm] + [app.common.uuid :as uuid] [clojure.set :as set] [clojure.string :as string] [clojure.test :as t])) @@ -1562,6 +1564,506 @@ ;; The width of copy-cp02-rect' is 25 (change is preserved) (t/is (= (:width copy-cp02-rect') 25)))) +(defn- switch-with-changes + "Switches the copy `shape-tag` to the variant `component-tag` keeping the + touched changes, as `tho/swap-component-in-shape` does, and returns the new + file and the changes." + [file shape-tag component-tag] + (let [page (thf/current-page file) + shape (ths/get-shape file shape-tag) + libraries {(:id file) file} + orig-shapes (cfh/get-children-with-self (:objects page) (:id shape)) + + [new-shape _ changes] + (cll/generate-component-swap (pcb/empty-changes) + (:objects page) + shape + (:data file) + page + libraries + (:id (thc/get-component file component-tag)) + 0 + nil + (select-keys shape ctk/swap-keep-attrs) + true) + + [changes _] (clv/generate-keep-touched changes new-shape shape orig-shapes + page libraries (:data file))] + [(thf/apply-changes file changes) changes])) + +(defn- check-switch-undone + "Undoes the switch `changes` on `file'` and checks that every shape of the + copy `shape-tag` is as it was in `file`, and that a redo gives `file'` + again." + [file file' changes shape-tag] + (let [file'' (thf/apply-undo-changes file' changes) + file''' (thf/apply-changes file'' changes) + objects (:objects (thf/current-page file)) + objects'' (:objects (thf/current-page file''))] + (doseq [shape (cfh/get-children-with-self objects (thi/id shape-tag))] + (t/is (= shape (get objects'' (:id shape))))) + (t/is (= (:objects (thf/current-page file')) + (:objects (thf/current-page file''')))))) + +(defn- setup-copy-with-swapped-child + "Variants `:c01` / `:c02` holding a copy of `:external01`, and a copy + `:copy01` of `:c01` whose `:external01` copy is swapped for `:external02` + (labelled `:copy-cp02`)." + [] + (-> (thf/sample-file :file1) + (tho/add-simple-component :external01 :external01-root :external01-child) + (tho/add-simple-component :external02 :external02-root :external02-child) + (thv/add-variant-with-copy :v01 :c01 :m01 :c02 :m02 :cp01 :cp02 :external01) + (thc/instantiate-component :c01 :copy01 :children-labels [:copy-cp01]) + (tho/swap-component-in-shape :copy-cp01 :external02 {:new-shape-label :copy-cp02 + :keep-touched? false}))) + +(t/deftest test-switch-variant-undo-keeps-swapped-child + (let [;; ==== Setup + file (setup-copy-with-swapped-child) + + ;; ==== Action + [file' changes] (switch-with-changes file :copy01 :c02)] + + ;; The new variant copy keeps the swapped child + (t/is (= (:shapes (ths/get-shape file' :copy01)) [(thi/id :copy-cp02)])) + ;; Undo restores the old variant copy with the swapped child + (check-switch-undone file file' changes :copy01))) + +(t/deftest test-switch-variant-undo-keeps-swapped-child-without-match + (let [;; ==== Setup + file (-> (setup-copy-with-swapped-child) + ;; The :external01 copy in :c02 gets another name, so + ;; the swapped child has no related shape in :c02 + (tho/change-shape :cp02 #(assoc % :name "Other child"))) + + ;; ==== Action + [file' changes] (switch-with-changes file :copy01 :c02)] + + ;; The swapped child is removed, and nothing is left on the root + (t/is (nil? (ths/get-shape-by-id file' (thi/id :copy-cp02)))) + (t/is (= (set (:shapes (ths/get-shape-by-id file uuid/zero))) + (set (:shapes (ths/get-shape-by-id file' uuid/zero))))) + ;; Undo restores the old variant copy with the swapped child + (check-switch-undone file file' changes :copy01))) + +(defn- label-child-by-name + "Gives the label `label` to the child named `child-name` of the shape + `parent-tag`." + [file parent-tag child-name label] + (let [child (->> (:shapes (ths/get-shape file parent-tag)) + (map #(ths/get-shape-by-id file %)) + (filter #(= child-name (:name %))) + first)] + (assert child (str "no child named " child-name)) + (thi/set-id! label (:id child)) + file)) + +(t/deftest test-switch-variant-undo-keeps-kept-and-dropped-swapped-children + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (tho/add-simple-component :external01 :external01-root :external01-child) + (tho/add-simple-component :external02 :external02-root :external02-child) + (thv/add-variant-with-copy :v01 :c01 :m01 :c02 :m02 :cp01 :cp02 :external01) + ;; A second :external01 copy in each variant, with other + ;; names, so the one in :c01 has no related shape in :c02 + (thc/instantiate-component :external01 :second-in-m01 :parent-label :m01) + (thc/instantiate-component :external01 :second-in-m02 :parent-label :m02) + (tho/change-shape :cp01 #(assoc % :name "First child")) + (tho/change-shape :cp02 #(assoc % :name "First child")) + (tho/change-shape :second-in-m01 #(assoc % :name "Second child")) + (tho/change-shape :second-in-m02 #(assoc % :name "Other child")) + (thc/instantiate-component :c01 :copy01) + (label-child-by-name :copy01 "First child" :first-in-copy) + (label-child-by-name :copy01 "Second child" :second-in-copy) + (tho/swap-component-in-shape :first-in-copy :external02 + {:new-shape-label :first-swapped + :keep-touched? false}) + (tho/swap-component-in-shape :second-in-copy :external02 + {:new-shape-label :second-swapped + :keep-touched? false})) + + ;; ==== Action + [file' changes] (switch-with-changes file :copy01 :c02) + copy01' (ths/get-shape file' :copy01)] + + ;; The swaps put each new child first, so the one to be removed comes + ;; first in the copy, and the switch processes it before the kept one + (t/is (= [(thi/id :second-swapped) (thi/id :first-swapped)] + (:shapes (ths/get-shape file :copy01)))) + ;; The first swapped child is kept, in the place of the first child of + ;; :c02, and the second one is removed + (t/is (= 2 (count (:shapes copy01')))) + (t/is (= (thi/id :first-swapped) (first (:shapes copy01')))) + (t/is (nil? (ths/get-shape-by-id file' (thi/id :second-swapped)))) + ;; Undo restores the old variant copy with both swapped children + (check-switch-undone file file' changes :copy01))) + +(defn- swap-at-index + "Swaps the shape `shape-tag` for a copy of `component-tag` in the same + place of its parent, as the workspace does (`tho/swap-component-in-shape` + puts it first). The new copy keeps the id, and so the label, of the shape." + [file shape-tag component-tag] + (let [page (thf/current-page file) + shape (ths/get-shape file shape-tag) + parent (get (:objects page) (:parent-id shape)) + index (first (keep-indexed #(when (= %2 (:id shape)) %1) (:shapes parent))) + + [_ _ changes] (cll/generate-component-swap (pcb/empty-changes) + (:objects page) + shape + (:data file) + page + {(:id file) file} + (:id (thc/get-component file component-tag)) + index + nil + (select-keys shape ctk/swap-keep-attrs) + false)] + (thf/apply-changes file changes))) + +(t/deftest test-switch-variant-undo-keeps-order-of-kept-and-dropped-swapped-children + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (tho/add-simple-component :external01 :external01-root :external01-child) + (tho/add-simple-component :external02 :external02-root :external02-child) + (thv/add-variant-with-copy :v01 :c01 :m01 :c02 :m02 :cp01 :cp02 :external01) + ;; :c01 holds a plain child, then one with no related + ;; shape in :c02, then one that matches the second + ;; child of :c02 + (thc/instantiate-component :external01 :second-in-m01 :parent-label :m01) + (thc/instantiate-component :external01 :third-in-m01 :parent-label :m01) + (thc/instantiate-component :external01 :second-in-m02 :parent-label :m02) + (tho/change-shape :cp01 #(assoc % :name "Plain child")) + (tho/change-shape :second-in-m01 #(assoc % :name "Dropped child")) + (tho/change-shape :third-in-m01 #(assoc % :name "Kept child")) + (tho/change-shape :cp02 #(assoc % :name "Plain child")) + (tho/change-shape :second-in-m02 #(assoc % :name "Kept child")) + (thc/instantiate-component :c01 :copy01) + (label-child-by-name :copy01 "Plain child" :plain-in-copy) + (label-child-by-name :copy01 "Dropped child" :dropped-in-copy) + (label-child-by-name :copy01 "Kept child" :kept-in-copy) + (swap-at-index :dropped-in-copy :external02) + (swap-at-index :kept-in-copy :external02)) + + ;; ==== Action + [file' changes] (switch-with-changes file :copy01 :c02) + copy01' (ths/get-shape file' :copy01)] + + ;; The swapped children keep their place in the copy + (t/is (= [(thi/id :plain-in-copy) (thi/id :dropped-in-copy) (thi/id :kept-in-copy)] + (:shapes (ths/get-shape file :copy01)))) + ;; The kept child takes the place of the second child of :c02, and the + ;; other swapped child is removed + (t/is (= 2 (count (:shapes copy01')))) + (t/is (= (thi/id :kept-in-copy) (second (:shapes copy01')))) + (t/is (nil? (ths/get-shape-by-id file' (thi/id :dropped-in-copy)))) + ;; Undo restores the old variant copy, with its children in their order + (check-switch-undone file file' changes :copy01))) + +(t/deftest test-nested-switch-in-copy-twice-keeps-copy + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (tho/add-frame :m03) + (thc/instantiate-component :c01 :nested01 :parent-label :m03) + (thc/make-component :c03 :m03) + (thc/instantiate-component :c03 :nested02 :children-labels [:child01]) + ;; After this switch the copy has a swap slot + (tho/swap-component-in-shape :child01 :c02 {:keep-touched? true})) + c01 (thc/get-component file :c01) + + ;; ==== Action + [file' changes] (switch-with-changes file :child01 :c01) + child01' (ths/get-shape file' :child01)] + + (t/is (some? (ctk/get-swap-slot (ths/get-shape file :child01)))) + ;; The second switch keeps the copy, now of :c01 + (t/is (= (:component-id child01') (:id c01))) + ;; Undo restores the copy of :c02 + (check-switch-undone file file' changes :child01))) + +(defn- setup-variant-with-copy-in-outer + "Variants `:c01` / `:c02` holding a copy of `:external01`, and a component + `:outer` whose main holds a copy of `:c01`. A copy of `:outer` is placed, + with its nested variant copy labelled `:c01-in-copy`, the child of that one + `:child-in-copy` and its child `:child-child`. + + `swap-in` says where the `:external01` copy is swapped for `:external02`: + `:copy` (in the nested variant copy of the copy of `:outer`), + `:outer-main` (in the main of `:outer`) or `:variant-main` (in `:m01`). + With `delete-outer`, `:outer` is deleted after the swap. + + With `outer-main-fills` (and `swap-in` `:outer-main`), the rect of the + swapped child in the main of `:outer` (labelled `:rect-in-outer-main`) + gets those fills. + + With `outer-of-outer`, the copy of `:outer` is not placed on its own but + inside a copy of a component `:outer2`, whose main holds a copy of `:outer`. + + With `nested-swap-in-copy`, `:external02` holds a copy of `:z01`, and the one + inside `:child-in-copy` (labelled `:z-in-copy`) is swapped in the copy for + `:z02` (new label `:z-swapped-in-copy`)." + [& {:keys [swap-in delete-outer outer-of-outer nested-swap-in-copy outer-main-fills]}] + (let [file (-> (thf/sample-file :file1) + (tho/add-simple-component :external01 :external01-root :external01-child) + (tho/add-simple-component :external02 :external02-root :external02-child)) + file (cond-> file + nested-swap-in-copy + (-> (tho/add-simple-component :z01 :z01-root :z01-child) + (tho/add-simple-component :z02 :z02-root :z02-child) + (thc/instantiate-component :z01 :z01-in-external02 + :parent-label :external02-root))) + file (thv/add-variant-with-copy file + :v01 :c01 :m01 :c02 :m02 :cp01 :cp02 :external01) + file (cond-> file + (= swap-in :variant-main) + (tho/swap-component-in-shape :cp01 :external02 {:new-shape-label :cp01-swapped + :keep-touched? false})) + file (-> file + (tho/add-simple-component :outer :outer-root :outer-child) + (thc/instantiate-component :c01 + :c01-in-outer-main + :children-labels [:child-in-outer-main] + :parent-label :outer-root)) + ;; Nothing propagates main changes to existing copies here, so the + ;; swap in the main of :outer must happen before its copy is placed + file (cond-> file + (= swap-in :outer-main) + (tho/swap-component-in-shape :child-in-outer-main :external02 + {:new-shape-label :swapped-in-outer-main + :keep-touched? false})) + file (if outer-main-fills + (let [page (thf/current-page file) + rect (-> (ths/get-shape file :swapped-in-outer-main) :shapes first)] + (thi/set-id! :rect-in-outer-main rect) + (thf/apply-changes file + (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) + #{rect} + #(assoc % :fills outer-main-fills) + (:objects page) + {}))) + file) + file (if outer-of-outer + (-> file + (tho/add-simple-component :outer2 :outer2-root :outer2-child) + (thc/instantiate-component :outer :outer-in-outer2-main + :parent-label :outer2-root) + (thc/instantiate-component :outer2 + :outer2-copy + :children-labels [:outer2-copy-rect :outer-copy + :outer-copy-rect :c01-in-copy + :child-in-copy :child-child + :z-in-copy])) + (thc/instantiate-component file + :outer + :outer-copy + :children-labels [:outer-copy-rect :c01-in-copy + :child-in-copy :child-child + :z-in-copy])) + file (cond-> file + (= swap-in :copy) + (tho/swap-component-in-shape :child-in-copy :external02 + {:new-shape-label :swapped-in-copy + :keep-touched? false}) + + nested-swap-in-copy + (tho/swap-component-in-shape :z-in-copy :z02 + {:new-shape-label :z-swapped-in-copy + :keep-touched? false}))] + (cond-> file + delete-outer + (tho/delete-shape :outer-root)))) + +(defn- switch-nested-variant + "Switches `:c01-in-copy` to `:c02` keeping the touched changes, and + returns the new file, the first child of the new variant copy and the + changes. The new copy keeps the id, and so the label, of the old one." + [file] + (let [[file' changes] (switch-with-changes file :c01-in-copy :c02) + c02-in-copy (ths/get-shape file' :c01-in-copy)] + [file' (ths/get-shape-by-id file' (-> c02-in-copy :shapes first)) changes])) + +(t/deftest test-switch-nested-variant-keeps-swap-made-in-copy + (let [;; ==== Setup + file (setup-variant-with-copy-in-outer :swap-in :copy) + external02 (thc/get-component file :external02) + swapped (ths/get-shape file :swapped-in-copy) + + ;; ==== Action + [file' new-child' changes] (switch-nested-variant file)] + + ;; The child of the nested variant copy was swapped for :external02 + (t/is (= (:component-id swapped) (:id external02))) + ;; The new variant copy keeps the swapped child, in the slot of the + ;; :external01 copy in :c02 + (t/is (= (:id new-child') (:id swapped))) + (t/is (= (:component-id new-child') (:id external02))) + (t/is (= (ctk/get-swap-slot new-child') (thi/id :cp02))) + ;; Undo restores the old variant copy with the swapped child + (check-switch-undone file file' changes :c01-in-copy))) + +(t/deftest test-switch-nested-variant-keeps-swap-made-in-copy-of-deleted-outer + (let [;; ==== Setup + file (setup-variant-with-copy-in-outer :swap-in :copy :delete-outer true) + external02 (thc/get-component file :external02) + swapped (ths/get-shape file :swapped-in-copy) + + ;; ==== Action + [_ new-child'] (switch-nested-variant file)] + + ;; The outer component is deleted + (t/is (nil? (thc/get-component file :outer))) + (t/is (some? (thc/get-component file :outer :include-deleted? true))) + ;; The child of the nested variant copy was swapped for :external02 + (t/is (= (:component-id swapped) (:id external02))) + ;; The new variant copy keeps the swapped child, in the slot of the + ;; :external01 copy in :c02 + (t/is (= (:id new-child') (:id swapped))) + (t/is (= (:component-id new-child') (:id external02))) + (t/is (= (ctk/get-swap-slot new-child') (thi/id :cp02))))) + +(defn- get-rect-child + "The rect child of `shape`, not a nested copy that :external02 may also hold" + [file shape] + (->> (:shapes shape) + (map #(ths/get-shape-by-id file %)) + (remove :component-id) + first)) + +(defn- check-swap-from-outer-main-kept + [file' new-child'] + (let [external02 (thc/get-component file' :external02) + new-child-child' (get-rect-child file' new-child')] + ;; The new variant copy shows :external02 instead of its default child + (t/is (= (:component-id new-child') (:id external02))) + ;; It points to the main of :external02, and takes the slot of the + ;; :external01 copy in :c02 + (t/is (= (:shape-ref new-child') (thi/id :external02-root))) + (t/is (= (ctk/get-swap-slot new-child') (thi/id :cp02))) + ;; The slots of its ref chain are not kept + (t/is (= 1 (count (filter ctk/swap-slot? (:touched new-child'))))) + ;; Its rect child points to the child of the main of :external02 + (t/is (= (:shape-ref new-child-child') (thi/id :external02-child))))) + +(t/deftest test-switch-nested-variant-keeps-swap-made-in-outer-main + (let [;; ==== Setup + file (setup-variant-with-copy-in-outer :swap-in :outer-main) + external02 (thc/get-component file :external02) + child-in-copy (ths/get-shape file :child-in-copy) + + ;; ==== Action + [file' new-child' changes] (switch-nested-variant file)] + + ;; The copy of :outer inherits the swap from its main + (t/is (= (:component-id child-in-copy) (:id external02))) + (check-swap-from-outer-main-kept file' new-child') + ;; Undo restores the old variant copy with the swapped child + (check-switch-undone file file' changes :c01-in-copy))) + +(t/deftest test-switch-nested-variant-keeps-swap-made-in-deleted-outer-main + (let [;; ==== Setup + file (setup-variant-with-copy-in-outer :swap-in :outer-main :delete-outer true) + external02 (thc/get-component file :external02) + child-in-copy (ths/get-shape file :child-in-copy) + + ;; ==== Action + [file' new-child'] (switch-nested-variant file)] + + ;; The outer component is deleted + (t/is (nil? (thc/get-component file :outer))) + (t/is (some? (thc/get-component file :outer :include-deleted? true))) + ;; The copy of :outer inherits the swap from its main + (t/is (= (:component-id child-in-copy) (:id external02))) + (check-swap-from-outer-main-kept file' new-child'))) + +(t/deftest test-switch-nested-variant-keeps-override-made-in-outer-main + (let [;; ==== Setup + fills (ths/sample-fills-color :fill-color "#fabada") + file (setup-variant-with-copy-in-outer :swap-in :outer-main + :outer-main-fills fills) + rect-in-outer-main (ths/get-shape file :rect-in-outer-main) + + ;; ==== Action + [file' new-child'] (switch-nested-variant file) + new-child-child' (get-rect-child file' new-child')] + + ;; The rect of the swapped child was overridden in the main of :outer + (t/is (contains? (:touched rect-in-outer-main) :fill-group)) + (check-swap-from-outer-main-kept file' new-child') + ;; The moved rect keeps the override, and it is still marked as touched + ;; now that it points to the child of the main of :external02 + (t/is (= (:fills new-child-child') fills)) + (t/is (contains? (:touched new-child-child') :fill-group)))) + +(t/deftest test-switch-nested-variant-drops-swap-made-in-variant-main + (let [;; ==== Setup + file (setup-variant-with-copy-in-outer :swap-in :variant-main) + external01 (thc/get-component file :external01) + external02 (thc/get-component file :external02) + child-in-copy (ths/get-shape file :child-in-copy) + + ;; ==== Action + [_ new-child'] (switch-nested-variant file)] + + ;; The swap is part of the definition of :c01 + (t/is (= (:component-id child-in-copy) (:id external02))) + ;; So the new variant copy shows the child of :c02 + (t/is (= (:component-id new-child') (:id external01))) + (t/is (= (:shape-ref new-child') (thi/id :cp02))))) + +(t/deftest test-switch-nested-variant-keeps-swap-made-in-outer-of-outer-main + (let [;; ==== Setup + file (setup-variant-with-copy-in-outer :swap-in :outer-main + :outer-of-outer true) + external02 (thc/get-component file :external02) + child-in-copy (ths/get-shape file :child-in-copy) + + ;; ==== Action + [file' new-child'] (switch-nested-variant file)] + + ;; The swap comes from the main of :outer, two levels up the ref chain + (t/is (= (:component-id child-in-copy) (:id external02))) + (check-swap-from-outer-main-kept file' new-child'))) + +(t/deftest test-switch-nested-variant-keeps-nested-swap-made-in-copy + (let [;; ==== Setup + file (setup-variant-with-copy-in-outer :swap-in :outer-main + :nested-swap-in-copy true) + z02 (thc/get-component file :z02) + z-swapped (ths/get-shape file :z-swapped-in-copy) + + ;; ==== Action + [file' new-child'] (switch-nested-variant file) + z-swapped' (ths/get-shape file' :z-swapped-in-copy)] + + (check-swap-from-outer-main-kept file' new-child') + ;; The copy of :z02 swapped inside the moved child moves with it, and + ;; keeps pointing to :z02, with the same slot + (t/is (= (:parent-id z-swapped') (:id new-child'))) + (t/is (= (:component-id z-swapped') (:id z02))) + (t/is (= (:shape-ref z-swapped') (thi/id :z02-root))) + (t/is (= (ctk/get-swap-slot z-swapped') (ctk/get-swap-slot z-swapped))))) + +(t/deftest test-switch-nested-variant-drops-swap-made-in-outer-main-without-match + (let [;; ==== Setup + file (-> (setup-variant-with-copy-in-outer :swap-in :outer-main) + ;; The :external01 copy in :c02 gets another name, so + ;; the swapped child has no related shape in :c02 + (tho/change-shape :cp02 #(assoc % :name "Other child"))) + external01 (thc/get-component file :external01) + + ;; ==== Action + [file' new-child' changes] (switch-nested-variant file)] + + ;; The new variant copy shows the child of :c02 + (t/is (= (:component-id new-child') (:id external01))) + (t/is (= (:shape-ref new-child') (thi/id :cp02))) + ;; The swapped child is removed with the old variant copy + (t/is (nil? (ths/get-shape-by-id file' (thi/id :child-in-copy)))) + ;; Undo restores the old variant copy with the swapped child + (check-switch-undone file file' changes :c01-in-copy))) + ;; ============================================================ ;; TOUCHED PARENT (switch without touched but with touched parent) ;; ============================================================ diff --git a/common/test/common_tests/runner.cljc b/common/test/common_tests/runner.cljc index a99feca75f..41056d8cb8 100644 --- a/common/test/common_tests/runner.cljc +++ b/common/test/common_tests/runner.cljc @@ -30,6 +30,7 @@ [common-tests.files-migrations-0038-test] [common-tests.files-migrations-0039-test] [common-tests.files-migrations-test] + [common-tests.files.changes-builder-test] [common-tests.files.comp-processors-test] [common-tests.files.helpers-test] [common-tests.files.page-diff-test] @@ -153,6 +154,7 @@ 'common-tests.files-migrations-0038-test 'common-tests.files-migrations-0039-test 'common-tests.files-migrations-test + 'common-tests.files.changes-builder-test 'common-tests.files.comp-processors-test 'common-tests.files.helpers-test 'common-tests.files.page-diff-test diff --git a/common/test/common_tests/types/file_test.cljc b/common/test/common_tests/types/file_test.cljc index deb89ef604..4cb0aeac1d 100644 --- a/common/test/common_tests/types/file_test.cljc +++ b/common/test/common_tests/types/file_test.cljc @@ -95,3 +95,55 @@ (t/is (= 1 (swap-level file :effects-copy 1))) ;; With no levels to look at, not even the shape itself is checked (t/is (= 0 (swap-level file :effects-in-mid 0))))) + +(defn- outer-swap + [file label base-label] + (let [page (thf/current-page file)] + (ctf/find-outer-swap (ctn/make-container page :page) + {(:id file) file} + (ths/get-shape file label) + (thi/id base-label)))) + +(t/deftest test-find-outer-swap-up-the-ref-chain + (let [file (setup-file)] + ;; Level 1 is the ref shape + (t/is (= [1 (thi/id :plain-in-inner)] (outer-swap file :effects-in-top :frame-inner))) + (t/is (= [2 (thi/id :plain-in-inner)] (outer-swap file :effects-copy :frame-inner))))) + +(t/deftest test-find-outer-swap-excludes-the-shape-itself + (let [file (setup-file)] + ;; :effects-in-mid is swapped, but its ref chain has no swap + (t/is (nil? (outer-swap file :effects-in-mid :frame-inner))))) + +(t/deftest test-find-outer-swap-stops-at-base + (let [file (setup-file)] + ;; The swap is made inside :frame-mid + (t/is (nil? (outer-swap file :effects-copy :frame-mid))))) + +(t/deftest test-find-outer-swap-none-found + (let [file (setup-file)] + ;; The ref chain of the copy has no swap and reaches the main + (t/is (nil? (outer-swap file :inner-copy :frame-base))) + ;; The base main has no ref chain at all + (t/is (nil? (outer-swap file :frame-base :frame-inner))))) + +(t/deftest test-find-outer-swap-in-deleted-component + (let [file (-> (setup-file) + (tho/delete-shape :frame-mid))] + (t/is (nil? (thc/get-component file :mid))) + (t/is (= [2 (thi/id :plain-in-inner)] (outer-swap file :effects-copy :frame-inner))))) + +(defn- outer-swaps + [file label] + (let [page (thf/current-page file)] + (ctf/get-outer-swaps (ctn/make-container page :page) + {(:id file) file} + (ths/get-shape file label)))) + +(t/deftest test-get-outer-swaps + (let [file (setup-file)] + ;; :effects-copy was swapped in :frame-mid, outside the main of :inner-copy + (t/is (= {(thi/id :effects-copy) [2 (thi/id :plain-in-inner)]} + (outer-swaps file :inner-copy))) + ;; but inside the main of :mid-copy, so it is part of its definition + (t/is (= {} (outer-swaps file :mid-copy))))) diff --git a/frontend/src/app/main/data/workspace/clipboard.cljs b/frontend/src/app/main/data/workspace/clipboard.cljs index c8789c42c0..55cb32fb36 100644 --- a/frontend/src/app/main/data/workspace/clipboard.cljs +++ b/frontend/src/app/main/data/workspace/clipboard.cljs @@ -70,42 +70,19 @@ [potok.v2.core :as ptk] [promesa.core :as p])) -(defn- advance-shape - [file libraries page level-delta head-id objects shape] - (let [new-shape-ref (ctf/advance-shape-ref file page libraries shape level-delta {:include-deleted? true}) - container (ctn/make-container page :page) - ;; The head becomes a root copy, which cannot carry a swap slot, so - ;; strip inherited slots only for it. Nested sub-heads keep them so - ;; they still pair with the children of the new near main. - head? (= (:id shape) head-id) - new-touched (ctf/get-touched-from-ref-chain-until-target-ref - container libraries shape new-shape-ref :remove-slots? head?)] - (cond-> objects - (and (some? new-shape-ref) (not= new-shape-ref (:shape-ref shape))) - (update (:id shape) assoc :shape-ref new-shape-ref :touched new-touched)))) - -(defn- advance-subtree - "Advance the shape and its children. A head swapped in one of the levels - being dropped already points to the new component main from that level, - so it and its children advance only up to there." - [file libraries page head-id level-delta objects shape] - (let [level-delta (if (ctk/instance-head? shape) - (ctf/get-swap-level (ctn/make-container page :page) - libraries shape level-delta) - level-delta)] - (if (pos? level-delta) - (reduce #(advance-subtree file libraries page head-id level-delta %1 - (get %1 %2)) - (advance-shape file libraries page level-delta head-id objects shape) - (:shapes shape)) - objects))) - (defn- advance-copy + "Advance the refs of the copy and its children. The head becomes a root + copy, which cannot carry a swap slot, so only the head loses the inherited + slots. Nested sub-heads keep them so they still pair with the children of + the new near main." [file libraries page objects shape] (if (and (ctk/instance-head? shape) (not (ctk/main-instance? shape))) - (advance-subtree file libraries page (:id shape) - (ctn/get-nesting-level-delta (:objects page) shape uuid/zero) - objects shape) + (reduce-kv (fn [objects id refs] + (update objects id merge refs)) + objects + (ctf/get-advanced-refs file (ctn/make-container page :page) libraries (:id shape) + (ctn/get-nesting-level-delta (:objects page) shape uuid/zero) + shape)) objects)) (defn advance-copies