mirror of
https://github.com/penpot/penpot.git
synced 2026-11-21 09:42:01 +00:00
♻️ Address review of the nested swap slot fixes
When pasting, read each child to advance from the objects being built, not from the page, so a later change to the parent cannot make it read a stale shape. Fix the docs of ignore-children-fn in get-children-ids and delete-shapes: it receives a shape id, not a shape. Add a unit test for ctf/get-swap-level, a paste test where the swap sits at the last level the copied head drops, and a detach test where the swapped copy sits under two non-swapped nested copies. AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
3df7c5fea2
commit
2f86049377
@ -160,7 +160,7 @@
|
||||
([objects id]
|
||||
(get-children-ids objects id {}))
|
||||
([objects id {:keys [ignore-children-fn]
|
||||
;;ignore-children-fn should receive a shape and return a boolean
|
||||
;;ignore-children-fn should receive a shape id and return a boolean
|
||||
:or {ignore-children-fn (constantly false)}}]
|
||||
(letfn [(collect [result id processed]
|
||||
(if (contains? processed id)
|
||||
|
||||
@ -245,8 +245,8 @@
|
||||
allow-altering-copies
|
||||
;; We will delete the shapes and its descendants.
|
||||
;; ignore-children-fn is used to ignore some descendants
|
||||
;; on the deletion process. It should receive a shape and
|
||||
;; return a boolean
|
||||
;; on the deletion process. It should receive a shape id
|
||||
;; and return a boolean
|
||||
ignore-children-fn
|
||||
ignore-mask
|
||||
ignore-flows-for]
|
||||
|
||||
@ -277,6 +277,76 @@
|
||||
(t/is (= (:shape-ref copy-nested-h-rectangle) (thi/id :nested-h-rectangle)))
|
||||
(t/is (= (:shape-ref copy-nested-rectangle) (thi/id :nested-rectangle)))))
|
||||
|
||||
(t/deftest test-dont-advance-under-deep-swapped-nested-copy
|
||||
(let [;; ==== Setup
|
||||
;; {:huge-board} [:name Huge Board] # [Component :c-huge-board]
|
||||
;; :big-board-in-huge [:name Big Board] @--> :big-board
|
||||
;; :h-board-in-huge [:name Board with ellipse] @--> :h-board-with-ellipse
|
||||
;; :nested3-h-ellipse [:name Ellipse] @--> :nested2-h-ellipse
|
||||
;; :nested3-ellipse [:name Ellipse] ---> :nested2-ellipse
|
||||
;; :copy-huge-board [:name Huge Board] #--> :huge-board
|
||||
;; :copy-big-board [:name Big Board] @--> :big-board-in-huge
|
||||
;; :copy-h-board-with-ellipse [:name Board with ellipse] @--> :h-board-in-huge
|
||||
;; :copy-board-with-rectangle [:name Board with rectangle, :swap-slot-label :nested-h-ellipse] @--> :board-with-rectangle
|
||||
;; :copy-nested-h-rectangle [:name Rectangle] @--> :nested-h-rectangle
|
||||
;; :copy-nested-rectangle [:name rectangle] ---> :nested-rectangle
|
||||
file (-> (setup-file)
|
||||
(tho/add-frame :huge-board :name "Huge Board")
|
||||
(thc/instantiate-component :c-big-board
|
||||
:big-board-in-huge
|
||||
:parent-label :huge-board
|
||||
:children-labels [:h-board-in-huge
|
||||
:nested3-h-ellipse
|
||||
:nested3-ellipse])
|
||||
(thc/make-component :c-huge-board :huge-board)
|
||||
(thc/instantiate-component :c-huge-board
|
||||
:copy-huge-board
|
||||
:children-labels [:copy-big-board
|
||||
:copy-h-board-with-ellipse
|
||||
:copy-nested-h-ellipse
|
||||
:copy-nested-ellipse])
|
||||
(thc/component-swap :copy-nested-h-ellipse
|
||||
:c-board-with-rectangle
|
||||
:copy-board-with-rectangle
|
||||
:children-labels [:copy-nested-h-rectangle
|
||||
:copy-nested-rectangle]))
|
||||
|
||||
page (thf/current-page file)
|
||||
|
||||
;; ==== Action
|
||||
changes (cll/generate-detach-instance (-> (pcb/empty-changes nil)
|
||||
(pcb/with-page page)
|
||||
(pcb/with-objects (:objects page)))
|
||||
page
|
||||
{(:id file) file}
|
||||
(thi/id :copy-huge-board))
|
||||
file' (thf/apply-changes file changes)
|
||||
|
||||
;; ==== Get
|
||||
copy-big-board (ths/get-shape file' :copy-big-board)
|
||||
copy-h-board-with-ellipse (ths/get-shape file' :copy-h-board-with-ellipse)
|
||||
copy-board-with-rectangle (ths/get-shape file' :copy-board-with-rectangle)
|
||||
copy-nested-h-rectangle (ths/get-shape file' :copy-nested-h-rectangle)
|
||||
copy-nested-rectangle (ths/get-shape file' :copy-nested-rectangle)]
|
||||
|
||||
;; ==== Check
|
||||
|
||||
;; The first level copy becomes a root copy and advances one level.
|
||||
(t/is (ctk/instance-root? copy-big-board))
|
||||
(t/is (= (:shape-ref copy-big-board) (thi/id :big-board)))
|
||||
(t/is (nil? (ctk/get-swap-slot copy-big-board)))
|
||||
|
||||
;; The non-swapped nested copy under it advances one level too.
|
||||
(t/is (= (:shape-ref copy-h-board-with-ellipse) (thi/id :h-board-with-ellipse)))
|
||||
|
||||
;; The nested copy swapped two levels down already points to the main
|
||||
;; of board-with-rectangle, so it and its children must not advance.
|
||||
(t/is (= (:shape-ref copy-board-with-rectangle) (thi/id :board-with-rectangle)))
|
||||
(t/is (= (ctk/get-swap-slot copy-board-with-rectangle) (thi/id :nested-h-ellipse)))
|
||||
|
||||
(t/is (= (:shape-ref copy-nested-h-rectangle) (thi/id :nested-h-rectangle)))
|
||||
(t/is (= (:shape-ref copy-nested-rectangle) (thi/id :nested-rectangle)))))
|
||||
|
||||
(t/deftest test-propagate-slot-when-swapped-main
|
||||
(let [;; ==== Setup
|
||||
file (-> (setup-file)
|
||||
|
||||
@ -109,6 +109,7 @@
|
||||
[common-tests.types.components-test]
|
||||
[common-tests.types.container-test]
|
||||
[common-tests.types.file-load-component-objects-test]
|
||||
[common-tests.types.file-test]
|
||||
[common-tests.types.fill-test]
|
||||
[common-tests.types.font-test]
|
||||
[common-tests.types.modifiers-test]
|
||||
@ -230,6 +231,7 @@
|
||||
'common-tests.types.components-test
|
||||
'common-tests.types.container-test
|
||||
'common-tests.types.file-load-component-objects-test
|
||||
'common-tests.types.file-test
|
||||
'common-tests.types.fill-test
|
||||
'common-tests.types.font-test
|
||||
'common-tests.types.modifiers-test
|
||||
|
||||
97
common/test/common_tests/types/file_test.cljc
Normal file
97
common/test/common_tests/types/file_test.cljc
Normal file
@ -0,0 +1,97 @@
|
||||
;; 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.types.file-test
|
||||
(:require
|
||||
[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]
|
||||
[app.common.types.container :as ctn]
|
||||
[app.common.types.file :as ctf]
|
||||
[clojure.test :as t]))
|
||||
|
||||
(t/use-fixtures :each thi/test-fixture)
|
||||
|
||||
(defn- setup-file
|
||||
[]
|
||||
;; {:frame-base} [:name frame-base] # [Component :base]
|
||||
;; {:frame-effects} [:name frame-effects] # [Component :effects]
|
||||
;; :base-in-effects [:name frame-base] @--> frame-base
|
||||
;; {:frame-plain} [:name frame-plain] # [Component :plain]
|
||||
;; {:frame-inner} [:name frame-inner] # [Component :inner]
|
||||
;; :plain-in-inner [:name frame-plain] @--> frame-plain
|
||||
;; {:frame-mid} [:name frame-mid] # [Component :mid]
|
||||
;; :inner-in-mid [:name frame-inner] @--> frame-inner
|
||||
;; :effects-in-mid [:name frame-effects, :swap-slot-label :plain-in-inner] @--> frame-effects
|
||||
;; [:name frame-base] @--> :base-in-effects
|
||||
;; {:frame-top} [:name frame-top] # [Component :top]
|
||||
;; :mid-in-top [:name frame-mid] @--> frame-mid
|
||||
;; :inner-in-top [:name frame-inner] @--> :inner-in-mid
|
||||
;; :effects-in-top [:name frame-effects] @--> :effects-in-mid
|
||||
;; :base-in-top [:name frame-base] @--> (base in :effects-in-mid)
|
||||
;; :top-copy [:name frame-top] #--> frame-top
|
||||
;; :mid-copy [:name frame-mid] @--> :mid-in-top
|
||||
;; :inner-copy [:name frame-inner] @--> :inner-in-top
|
||||
;; :effects-copy [:name frame-effects] @--> :effects-in-top
|
||||
;; :base-copy [:name frame-base] @--> :base-in-top
|
||||
(-> (thf/sample-file :file1)
|
||||
(tho/add-frame :frame-base :name "frame-base")
|
||||
(thc/make-component :base :frame-base)
|
||||
(tho/add-frame :frame-effects :name "frame-effects")
|
||||
(thc/make-component :effects :frame-effects)
|
||||
(thc/instantiate-component :base :base-in-effects :parent-label :frame-effects)
|
||||
(tho/add-frame :frame-plain :name "frame-plain")
|
||||
(thc/make-component :plain :frame-plain)
|
||||
(tho/add-frame :frame-inner :name "frame-inner")
|
||||
(thc/make-component :inner :frame-inner)
|
||||
(thc/instantiate-component :plain :plain-in-inner :parent-label :frame-inner)
|
||||
(tho/add-frame :frame-mid :name "frame-mid")
|
||||
(thc/make-component :mid :frame-mid)
|
||||
(thc/instantiate-component :inner :inner-in-mid :parent-label :frame-mid
|
||||
:children-labels [:plain-in-mid])
|
||||
(thc/component-swap :plain-in-mid :effects :effects-in-mid)
|
||||
(tho/add-frame :frame-top :name "frame-top")
|
||||
(thc/make-component :top :frame-top)
|
||||
(thc/instantiate-component :mid :mid-in-top :parent-label :frame-top
|
||||
:children-labels [:inner-in-top :effects-in-top :base-in-top])
|
||||
(thc/instantiate-component :top :top-copy
|
||||
:children-labels [:mid-copy :inner-copy
|
||||
:effects-copy :base-copy])))
|
||||
|
||||
(defn- swap-level
|
||||
[file label max-levels]
|
||||
(let [page (thf/current-page file)]
|
||||
(ctf/get-swap-level (ctn/make-container page :page)
|
||||
{(:id file) file}
|
||||
(ths/get-shape file label)
|
||||
max-levels)))
|
||||
|
||||
(t/deftest test-get-swap-level-shape-swapped-itself
|
||||
(let [file (setup-file)]
|
||||
(t/is (= 0 (swap-level file :effects-in-mid 5)))))
|
||||
|
||||
(t/deftest test-get-swap-level-swapped-up-the-ref-chain
|
||||
(let [file (setup-file)]
|
||||
(t/is (= 1 (swap-level file :effects-in-top 5)))
|
||||
(t/is (= 2 (swap-level file :effects-copy 5)))))
|
||||
|
||||
(t/deftest test-get-swap-level-none-found
|
||||
(let [file (setup-file)]
|
||||
;; The ref chain of the copy has no swap and reaches the main
|
||||
(t/is (= 5 (swap-level file :inner-copy 5)))
|
||||
;; The base main has no ref chain at all
|
||||
(t/is (= 5 (swap-level file :frame-base 5)))))
|
||||
|
||||
(t/deftest test-get-swap-level-capped-by-max-levels
|
||||
(let [file (setup-file)]
|
||||
;; The swap sits exactly at max-levels
|
||||
(t/is (= 2 (swap-level file :effects-copy 2)))
|
||||
;; The swap sits beyond max-levels
|
||||
(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)))))
|
||||
@ -95,7 +95,7 @@
|
||||
level-delta)]
|
||||
(if (pos? level-delta)
|
||||
(reduce #(advance-subtree file libraries page head-id level-delta %1
|
||||
(get (:objects page) %2))
|
||||
(get %1 %2))
|
||||
(advance-shape file libraries page level-delta head-id objects shape)
|
||||
(:shapes shape))
|
||||
objects)))
|
||||
|
||||
@ -1297,3 +1297,101 @@
|
||||
|
||||
;; the file must be valid after the paste
|
||||
(t/is (nil? errors))))))))
|
||||
|
||||
(defn- setup-file-swap-in-mid
|
||||
[]
|
||||
;; {:frame-base} [:name frame-base] # [Component :base]
|
||||
;; {:frame-effects} [:name frame-effects] # [Component :effects]
|
||||
;; :base-in-effects [:name frame-base] @--> frame-base
|
||||
;; {:frame-plain} [:name frame-plain] # [Component :plain]
|
||||
;; {:frame-inner} [:name frame-inner] # [Component :inner]
|
||||
;; :plain-in-inner [:name frame-plain] @--> frame-plain
|
||||
;; {:frame-mid} [:name frame-mid] # [Component :mid]
|
||||
;; :inner-in-mid [:name frame-inner] @--> frame-inner
|
||||
;; :effects-in-mid [:name frame-effects, :swap-slot-label :plain-in-inner] @--> frame-effects
|
||||
;; [:name frame-base] @--> :base-in-effects
|
||||
;; {:frame-top} [:name frame-top] # [Component :top]
|
||||
;; :mid-in-top [:name frame-mid] @--> frame-mid
|
||||
;; :inner-in-top [:name frame-inner] @--> :inner-in-mid
|
||||
;; :effects-in-top [:name frame-effects] @--> :effects-in-mid
|
||||
;; [:name frame-base] @--> (base in :effects-in-mid)
|
||||
;; :top-copy [:name frame-top] #--> frame-top
|
||||
;; :mid-copy [:name frame-mid] @--> :mid-in-top
|
||||
;; :inner-copy [:name frame-inner] @--> :inner-in-top
|
||||
;; :effects-copy [:name frame-effects] @--> :effects-in-top
|
||||
;; :base-copy [:name frame-base] @--> (base in :effects-in-top)
|
||||
(-> (cthf/sample-file :file1)
|
||||
(ctho/add-frame :frame-base :name "frame-base")
|
||||
(cthc/make-component :base :frame-base)
|
||||
(ctho/add-frame :frame-effects :name "frame-effects")
|
||||
(cthc/make-component :effects :frame-effects)
|
||||
(cthc/instantiate-component :base :base-in-effects :parent-label :frame-effects)
|
||||
(ctho/add-frame :frame-plain :name "frame-plain")
|
||||
(cthc/make-component :plain :frame-plain)
|
||||
(ctho/add-frame :frame-inner :name "frame-inner")
|
||||
(cthc/make-component :inner :frame-inner)
|
||||
(cthc/instantiate-component :plain :plain-in-inner :parent-label :frame-inner)
|
||||
(ctho/add-frame :frame-mid :name "frame-mid")
|
||||
(cthc/make-component :mid :frame-mid)
|
||||
(cthc/instantiate-component :inner :inner-in-mid :parent-label :frame-mid
|
||||
:children-labels [:plain-in-mid])
|
||||
(cthc/component-swap :plain-in-mid :effects :effects-in-mid)
|
||||
(ctho/add-frame :frame-top :name "frame-top")
|
||||
(cthc/make-component :top :frame-top)
|
||||
(cthc/instantiate-component :mid :mid-in-top :parent-label :frame-top
|
||||
:children-labels [:inner-in-top :effects-in-top])
|
||||
(cthc/instantiate-component :top :top-copy
|
||||
:children-labels [:mid-copy :inner-copy
|
||||
:effects-copy :base-copy])))
|
||||
|
||||
(t/deftest test-keep-swapped-children-refs-copy-paste-swap-at-last-dropped-level
|
||||
(t/async
|
||||
done
|
||||
(let [;; ==== Setup
|
||||
file (setup-file-swap-in-mid)
|
||||
store (ths/setup-store file)
|
||||
|
||||
;; ==== Action
|
||||
page (cthf/current-page file)
|
||||
inner (cths/get-shape file :inner-copy)
|
||||
features #{"components/v2"}
|
||||
version 47
|
||||
|
||||
pdata (thp/simulate-copy-shape #{(:id inner)} (:objects page) {(:id file) file} page file features version)
|
||||
|
||||
events
|
||||
[(dws/select-shape uuid/zero)
|
||||
(dw/paste-shapes pdata)]]
|
||||
|
||||
(ths/run-store
|
||||
store done events
|
||||
(fn [new-state]
|
||||
(let [;; ==== Get
|
||||
file' (ths/get-file-from-state new-state)
|
||||
page' (cthf/current-page file')
|
||||
inner' (cths/get-shape file' :inner-copy)
|
||||
effects' (cths/get-shape file' :effects-copy)
|
||||
base' (cths/get-shape file' :base-copy)
|
||||
copied-inner' (find-copied-shape inner' page' uuid/zero)
|
||||
copied-effects' (find-copied-shape effects' page' (:id copied-inner'))
|
||||
copied-base' (find-copied-shape base' page' (:id copied-effects'))
|
||||
errors (try
|
||||
(cfv/validate-file! file' {(:id file') file'})
|
||||
nil
|
||||
(catch :default cause
|
||||
(ex-data cause)))]
|
||||
|
||||
;; ==== Check
|
||||
;; the swap was made in the mid main, the last of the two levels
|
||||
;; the copied inner drops, so the children of the swapped copy
|
||||
;; advance both levels, up to the effects main
|
||||
(t/is (some? copied-inner'))
|
||||
(t/is (= (:shape-ref copied-inner') (cthi/id :frame-inner)))
|
||||
(t/is (some? copied-effects'))
|
||||
(t/is (= (:shape-ref copied-effects') (cthi/id :frame-effects)))
|
||||
(t/is (= (ctk/get-swap-slot copied-effects') (cthi/id :plain-in-inner)))
|
||||
(t/is (some? copied-base'))
|
||||
(t/is (= (:shape-ref copied-base') (cthi/id :base-in-effects)))
|
||||
|
||||
;; the file must be valid after the paste
|
||||
(t/is (nil? errors))))))))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user