mirror of
https://github.com/penpot/penpot.git
synced 2026-11-22 10:20:51 +00:00
A copy child taken out of its layout's flow stays where the layout put it, which can differ from its main's place when the copy has its own layout values. Making it absolute now marks its geometry as an override too, so sync keeps that place instead of moving it to the main's. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | world | baseline | now | ratio | |------------------------------------+----------+--------+-------| | deepest-edit-through-three-levels | 35.215 | 18.081 | 0.51 | | edit-through-a-chain-of-dependents | 68.931 | 35.42 | 0.51 | | library-update-into-copies | 83.083 | 45.335 | 0.55 | | main-edit-among-many-shapes | 4.837 | 2.3 | 0.48 | | main-edit-into-10-copies | 1.79 | 0.905 | 0.51 | | main-edit-into-100-copies | 15.415 | 8.714 | 0.57 | | main-edit-into-1000-copies | 153.086 | 88.228 | 0.58 | | variant-switch-into-copies | 32.052 | 20.752 | 0.65 | AI-assisted-by: claude-opus-5-5
371 lines
18 KiB
Clojure
371 lines
18 KiB
Clojure
;; 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.comp-touched-test
|
|
(:require
|
|
[app.common.files.changes-builder :as pcb]
|
|
[app.common.logic.shapes :as cls]
|
|
[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)
|
|
|
|
(t/deftest test-touched-when-changing-attribute
|
|
(let [;; ==== Setup
|
|
file (-> (thf/sample-file :file1)
|
|
(tho/add-simple-component-with-copy :component1
|
|
:main-root
|
|
:main-child
|
|
:copy-root
|
|
:main-child-params {:fills (ths/sample-fills-color
|
|
:fill-color "#abcdef")}
|
|
:copy-root-params {:children-labels [:copy-child]}))
|
|
page (thf/current-page file)
|
|
copy-child (ths/get-shape file :copy-child)
|
|
|
|
;; ==== Action
|
|
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
|
|
#{(:id copy-child)}
|
|
(fn [shape]
|
|
(assoc shape :fills (ths/sample-fills-color :fill-color "#fabada")))
|
|
(:objects page)
|
|
{})
|
|
|
|
file' (thf/apply-changes file changes)
|
|
|
|
;; ==== Get
|
|
copy-root' (ths/get-shape file' :copy-root)
|
|
copy-child' (ths/get-shape file' :copy-child)
|
|
fills' (:fills copy-child')
|
|
fill' (first fills')]
|
|
|
|
;; ==== Check
|
|
(t/is (some? copy-root'))
|
|
(t/is (some? copy-child'))
|
|
(t/is (= (count fills') 1))
|
|
(t/is (= (:fill-color fill') "#fabada"))
|
|
(t/is (= (:fill-opacity fill') 1))
|
|
(t/is (= (:touched copy-root') nil))
|
|
(t/is (= (:touched copy-child') #{:fill-group}))))
|
|
|
|
(t/deftest test-touched-from-library
|
|
(let [;; ==== Setup
|
|
library (-> (thf/sample-file :library :is-shared true)
|
|
(tho/add-simple-component :component1 :main-root :main-child
|
|
:child-params {:fills (ths/sample-fills-color
|
|
:fill-color "#abcdef")}))
|
|
|
|
file (-> (thf/sample-file :file)
|
|
(thc/instantiate-component :component1 :copy-root
|
|
:library library
|
|
:children-labels [:copy-child]))
|
|
|
|
page (thf/current-page file)
|
|
copy-child (ths/get-shape file :copy-child)
|
|
|
|
;; ==== Action
|
|
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
|
|
#{(:id copy-child)}
|
|
(fn [shape]
|
|
(assoc shape :fills (ths/sample-fills-color :fill-color "#fabada")))
|
|
(:objects page)
|
|
{})
|
|
|
|
file' (thf/apply-changes file changes)
|
|
|
|
;; ==== Get
|
|
copy-root' (ths/get-shape file' :copy-root)
|
|
copy-child' (ths/get-shape file' :copy-child)
|
|
fills' (:fills copy-child')
|
|
fill' (first fills')]
|
|
|
|
;; ==== Check
|
|
(t/is (some? copy-root'))
|
|
(t/is (some? copy-child'))
|
|
(t/is (= (count fills') 1))
|
|
(t/is (= (:fill-color fill') "#fabada"))
|
|
(t/is (= (:fill-opacity fill') 1))
|
|
(t/is (= (:touched copy-root') nil))
|
|
(t/is (= (:touched copy-child') #{:fill-group}))))
|
|
|
|
(t/deftest test-not-touched-when-adding-shape
|
|
(let [;; ==== Setup
|
|
file (-> (thf/sample-file :file1)
|
|
(tho/add-simple-component-with-copy :component1
|
|
:main-root
|
|
:main-child
|
|
:copy-root
|
|
:copy-root-params {:children-labels [:copy-child]})
|
|
(ths/add-sample-shape :free-shape))
|
|
|
|
page (thf/current-page file)
|
|
copy-root (ths/get-shape file :copy-root)
|
|
|
|
;; ==== Action
|
|
|
|
;; IMPORTANT: as modifying copies structure is now forbidden, this action
|
|
;; will not have any effect, and so the parent shape won't also be touched.
|
|
changes (cls/generate-relocate (-> (pcb/empty-changes nil)
|
|
(pcb/with-page-id (:id page))
|
|
(pcb/with-objects (:objects page)))
|
|
(thi/id :copy-root) ; parent-id
|
|
0 ; to-index
|
|
#{(thi/id :free-shape)}) ; ids
|
|
|
|
|
|
file' (thf/apply-changes file changes)
|
|
|
|
;; ==== Get
|
|
copy-root' (ths/get-shape file' :copy-root)
|
|
copy-child' (ths/get-shape file' :copy-child)]
|
|
|
|
;; ==== Check
|
|
(t/is (some? copy-root'))
|
|
(t/is (some? copy-child'))
|
|
(t/is (= (:touched copy-root') nil))
|
|
(t/is (= (:touched copy-child') nil))))
|
|
|
|
(t/deftest test-not-touched-when-deleting-shape
|
|
(let [;; ==== Setup
|
|
file (-> (thf/sample-file :file1)
|
|
(tho/add-simple-component-with-copy :component1
|
|
:main-root
|
|
:main-child
|
|
:copy-root
|
|
:copy-root-params {:children-labels [:copy-child]}))
|
|
|
|
page (thf/current-page file)
|
|
copy-child (ths/get-shape file :copy-child)
|
|
|
|
;; ==== Action
|
|
|
|
;; IMPORTANT: as modifying copies structure is now forbidden, this action will not
|
|
;; delete the child shape, but hide it (thus setting the visibility group).
|
|
[_all-parents changes]
|
|
(cls/generate-delete-shapes (pcb/empty-changes)
|
|
file
|
|
page
|
|
(:objects page)
|
|
#{(:id copy-child)}
|
|
{})
|
|
|
|
file' (thf/apply-changes file changes)
|
|
|
|
;; ==== Get
|
|
copy-root' (ths/get-shape file' :copy-root)
|
|
copy-child' (ths/get-shape file' :copy-child)]
|
|
|
|
;; ==== Check
|
|
(t/is (some? copy-root'))
|
|
(t/is (some? copy-child'))
|
|
(t/is (= (:touched copy-root') nil))
|
|
(t/is (= (:touched copy-child') #{:visibility-group}))))
|
|
|
|
(t/deftest test-not-touched-when-moving-shape
|
|
(let [;; ==== Setup
|
|
file (-> (thf/sample-file :file1)
|
|
(tho/add-component-with-many-children-and-copy :component1
|
|
:main-root
|
|
[:main-child1 :main-child2 :main-child3]
|
|
:copy-root
|
|
:copy-root-params {:children-labels [:copy-child1
|
|
:copy-child2
|
|
:copy-child3]})
|
|
(ths/add-sample-shape :free-shape))
|
|
|
|
page (thf/current-page file)
|
|
copy-child1 (ths/get-shape file :copy-child1)
|
|
|
|
;; ==== Action
|
|
|
|
;; IMPORTANT: as modifying copies structure is now forbidden, this action
|
|
;; will not have any effect, and so the parent shape won't also be touched.
|
|
changes (cls/generate-relocate (-> (pcb/empty-changes nil)
|
|
(pcb/with-page-id (:id page))
|
|
(pcb/with-objects (:objects page)))
|
|
(thi/id :copy-root) ; parent-id
|
|
2 ; to-index
|
|
#{(:id copy-child1)}) ; ids
|
|
|
|
|
|
file' (thf/apply-changes file changes)
|
|
|
|
;; ==== Get
|
|
copy-root' (ths/get-shape file' :copy-root)
|
|
copy-child1' (ths/get-shape file' :copy-child1)
|
|
copy-child2' (ths/get-shape file' :copy-child2)
|
|
copy-child3' (ths/get-shape file' :copy-child3)]
|
|
|
|
;; ==== Check
|
|
(t/is (some? copy-root'))
|
|
(t/is (some? copy-child1'))
|
|
(t/is (= (:touched copy-root') nil))
|
|
(t/is (= (:touched copy-child1') nil))
|
|
(t/is (= (:touched copy-child2') nil))
|
|
(t/is (= (:touched copy-child3') nil))
|
|
(t/is (= (first (:shapes copy-root')) (:id copy-child1')))
|
|
(t/is (= (second (:shapes copy-root')) (:id copy-child2')))
|
|
(t/is (= (nth (:shapes copy-root') 2) (:id copy-child3')))))
|
|
|
|
(t/deftest test-touched-when-changing-upper
|
|
(let [;; ==== Setup
|
|
file (-> (thf/sample-file :file1)
|
|
(tho/add-nested-component-with-copy :component1
|
|
:main1-root
|
|
:main1-child
|
|
:component2
|
|
:main2-root
|
|
:main2-nested-head
|
|
:copy2-root
|
|
:main2-root-params {:fills (ths/sample-fills-color
|
|
:fill-color "#abcdef")}))
|
|
page (thf/current-page file)
|
|
copy2-root (ths/get-shape file :copy2-root)
|
|
|
|
;; ==== Action
|
|
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
|
|
#{(:id copy2-root)}
|
|
(fn [shape]
|
|
(assoc shape :fills (ths/sample-fills-color :fill-color "#fabada")))
|
|
(:objects page)
|
|
{})
|
|
|
|
file' (thf/apply-changes file changes)
|
|
|
|
;; ==== Get
|
|
copy2-root' (ths/get-shape file' :copy2-root)
|
|
fills' (:fills copy2-root')
|
|
fill' (first fills')]
|
|
|
|
;; ==== Check
|
|
(t/is (some? copy2-root'))
|
|
(t/is (= (count fills') 1))
|
|
(t/is (= (:fill-color fill') "#fabada"))
|
|
(t/is (= (:fill-opacity fill') 1))
|
|
(t/is (= (:touched copy2-root') #{:fill-group}))))
|
|
|
|
(t/deftest test-touched-when-changing-lower
|
|
(let [;; ==== Setup
|
|
file (-> (thf/sample-file :file1)
|
|
(tho/add-nested-component-with-copy :component1
|
|
:main1-root
|
|
:main1-child
|
|
:component2
|
|
:main2-root
|
|
:main2-nested-head
|
|
:copy2-root
|
|
:copy2-root-params {:children-labels [:copy2-child]}))
|
|
page (thf/current-page file)
|
|
copy2-child (ths/get-shape file :copy2-child)
|
|
|
|
;; ==== Action
|
|
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
|
|
#{(:id copy2-child)}
|
|
(fn [shape]
|
|
(assoc shape :fills (ths/sample-fills-color :fill-color "#fabada")))
|
|
(:objects page)
|
|
{})
|
|
|
|
file' (thf/apply-changes file changes)
|
|
|
|
;; ==== Get
|
|
copy2-root' (ths/get-shape file' :copy2-root)
|
|
copy2-child' (ths/get-shape file' :copy2-child)
|
|
fills' (:fills copy2-child')
|
|
fill' (first fills')]
|
|
|
|
;; ==== Check
|
|
(t/is (some? copy2-root'))
|
|
(t/is (some? copy2-child'))
|
|
(t/is (= (count fills') 1))
|
|
(t/is (= (:fill-color fill') "#fabada"))
|
|
(t/is (= (:fill-opacity fill') 1))
|
|
(t/is (= (:touched copy2-root') nil))
|
|
(t/is (= (:touched copy2-child') #{:fill-group}))))
|
|
|
|
(t/deftest test-undo-of-a-touch-gives-back-remote-synced
|
|
;; a copy shape synced from a remote main (after a reset below the copy
|
|
;; root, say) loses :remote-synced when it is touched
|
|
(let [file (-> (thf/sample-file :file1)
|
|
(tho/add-simple-component-with-copy :component1 :main-root :main-child :copy-root
|
|
:copy-root-params {:children-labels [:copy-child]})
|
|
(ths/update-shape :copy-child :remote-synced true))
|
|
page (thf/current-page file)
|
|
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
|
|
#{(thi/id :copy-child)}
|
|
#(assoc % :opacity 0.5)
|
|
(:objects page)
|
|
{})
|
|
file' (thf/apply-changes file changes)
|
|
file'' (thf/apply-undo-changes file' changes)]
|
|
(t/is (nil? (:remote-synced (ths/get-shape file' :copy-child))))
|
|
(t/is (true? (:remote-synced (ths/get-shape file'' :copy-child))))
|
|
(t/is (nil? (:touched (ths/get-shape file'' :copy-child))))))
|
|
|
|
(t/deftest test-an-update-writes-touched-groups-whole
|
|
;; an update that sets the touched groups of a copy shape writes them with
|
|
;; the operation of their own, and its undo gives back the ones it had
|
|
(let [file (-> (thf/sample-file :file1)
|
|
(tho/add-simple-component-with-copy :component1 :main-root :main-child :copy-root
|
|
:copy-root-params {:children-labels [:copy-child]}))
|
|
page (thf/current-page file)
|
|
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
|
|
#{(thi/id :copy-child)}
|
|
#(assoc % :touched #{:shadow-group})
|
|
(:objects page)
|
|
{})
|
|
ops (mapcat :operations (:redo-changes changes))
|
|
file' (thf/apply-changes file changes)
|
|
file'' (thf/apply-undo-changes file' changes)]
|
|
(t/is (= [{:type :set-touched :touched #{:shadow-group}}] ops))
|
|
(t/is (= #{:shadow-group} (:touched (ths/get-shape file' :copy-child))))
|
|
(t/is (nil? (:touched (ths/get-shape file'' :copy-child))))))
|
|
|
|
(t/deftest test-touched-set-by-an-update-then-its-attrs-touch
|
|
;; an update sets the touched groups of a copy shape and changes an
|
|
;; attribute: the attribute touches its group on top of them, whatever the
|
|
;; order of the shape's keys
|
|
(doseq [[attr value group] [[:fills (ths/sample-fills-color :fill-color "#ff0000") :fill-group]
|
|
[:opacity 0.5 :layer-effects-group]
|
|
[:strokes [{:stroke-color "#ff0000" :stroke-opacity 1 :stroke-width 2}] :stroke-group]
|
|
[:hidden true :visibility-group]
|
|
[:blocked true :modifiable-group]
|
|
[:r1 5 :radius-group]
|
|
[:name "Renamed" :name-group]]]
|
|
(let [file (-> (thf/sample-file :file1)
|
|
(tho/add-simple-component-with-copy :component1 :main-root :main-child :copy-root
|
|
:copy-root-params {:children-labels [:copy-child]}))
|
|
page (thf/current-page file)
|
|
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
|
|
#{(thi/id :copy-child)}
|
|
#(assoc % attr value :touched #{:shadow-group})
|
|
(:objects page)
|
|
{})
|
|
file' (thf/apply-changes file changes)]
|
|
(t/is (= #{:shadow-group group} (:touched (ths/get-shape file' :copy-child))) (name attr)))))
|
|
|
|
|
|
(t/deftest a-copy-child-made-absolute-keeps-its-place-as-an-override
|
|
(let [file (-> (thf/sample-file :file1)
|
|
(tho/add-simple-component-with-copy :component1 :main-root :main-child :copy-root
|
|
:copy-root-params {:children-labels [:copy-child]})
|
|
(ths/update-shape :main-root :layout :flex)
|
|
(ths/update-shape :copy-root :layout :flex))
|
|
page (thf/current-page file)
|
|
copy-child (ths/get-shape file :copy-child)
|
|
changes (cls/generate-update-shapes (pcb/empty-changes nil (:id page))
|
|
#{(:id copy-child)}
|
|
#(assoc % :layout-item-absolute true)
|
|
(:objects page)
|
|
{})
|
|
file' (thf/apply-changes file changes)]
|
|
(t/is (= #{:layout-item-absolute :geometry-group}
|
|
(:touched (ths/get-shape file' :copy-child))))))
|