mirror of
https://github.com/penpot/penpot.git
synced 2026-11-20 00:50:34 +00:00
Which shapes of a copy a transform leaves untouched (the ignore tree) was frontend code, written twice: calculate-ignore-tree for the modifiers of the SVG renderer and calculate-ignore-tree-wasm for the matrices of the WASM renderer. The second is the single-pass version of the first: each shape still compares itself with the copy root of the latest transformed shape above it, as the repeated walks did when the last one won. app.common.logic.placement now holds it once: calculate-ignore-tree takes the transformed ids in order and a function that transforms a shape, built with modifiers-transform (SVG) or matrices-transform (WASM), both cached per shape. check-delta and transform-attrs moved with it. The frontend calls the common version, and its tests moved to common-tests.logic.placement-test, which runs on the JVM and in JS, with a rotation through modifiers added. No behavior change: the golden masters and the suites are unchanged. The benchmark does not run this code. AI-assisted-by: claude-opus-5-5
78 lines
3.5 KiB
Clojure
78 lines
3.5 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.placement-test
|
|
(:require
|
|
[app.common.files.helpers :as cfh]
|
|
[app.common.geom.matrix :as gmt]
|
|
[app.common.geom.modifiers :as gm]
|
|
[app.common.geom.point :as gpt]
|
|
[app.common.logic.placement :as clp]
|
|
[app.common.test-helpers.compositions :as tho]
|
|
[app.common.test-helpers.files :as thf]
|
|
[app.common.test-helpers.ids-map :as thi]
|
|
[app.common.types.component :as ctk]
|
|
[app.common.types.modifiers :as ctm]
|
|
[clojure.test :as t]))
|
|
|
|
(t/use-fixtures :each thi/test-fixture)
|
|
|
|
(defn- setup
|
|
[]
|
|
(let [file (-> (thf/sample-file :file1)
|
|
(tho/add-frame :board)
|
|
(tho/add-nested-component-with-copy :component1 :main1-root :main1-child
|
|
:component2 :main2-root :nested-head
|
|
:copy2-root
|
|
:copy2-root-params {:parent-label :board}))]
|
|
(:objects (thf/current-page file))))
|
|
|
|
(defn- moved-ignore-tree
|
|
"The ignore tree of moving the shapes `moved-ids` by (10, 20), each with
|
|
the same matrix, as the WASM renderer gives it."
|
|
[objects moved-ids]
|
|
(let [transforms (zipmap moved-ids (repeat (gmt/translate-matrix (gpt/point 10 20))))]
|
|
(clp/calculate-ignore-tree objects moved-ids (clp/matrices-transform transforms))))
|
|
|
|
(defn- copy-ids
|
|
[objects]
|
|
(->> (cfh/get-children-ids-with-self objects (thi/id :copy2-root))
|
|
(filter #(ctk/in-component-copy? (get objects %)))))
|
|
|
|
(t/deftest moving-a-board-keeps-its-copies-untouched
|
|
(let [objects (setup)
|
|
tree (moved-ignore-tree objects (cfh/get-children-ids-with-self objects (thi/id :board)))]
|
|
(t/is (seq (copy-ids objects)))
|
|
(t/is (every? #(true? (get tree %)) (copy-ids objects)))))
|
|
|
|
(t/deftest moving-a-copy-keeps-its-children-untouched
|
|
(let [objects (setup)
|
|
tree (moved-ignore-tree objects (cfh/get-children-ids-with-self objects (thi/id :copy2-root)))]
|
|
(t/is (every? #(true? (get tree %)) (copy-ids objects)))))
|
|
|
|
(t/deftest moving-a-shape-inside-a-copy-touches-it
|
|
(let [objects (setup)
|
|
rect-id (last (copy-ids objects))
|
|
tree (moved-ignore-tree objects [rect-id])]
|
|
(t/is (false? (get tree rect-id)))))
|
|
|
|
(t/deftest rotating-a-copy-with-modifiers-keeps-its-children-untouched
|
|
(let [file (-> (thf/sample-file :file1)
|
|
(tho/add-simple-component-with-copy :component1 :main-root :main-child :copy-root
|
|
:main-root-params {:width 100 :height 100}
|
|
:main-child-params {:x 10 :y 10 :width 20 :height 20}))
|
|
objects (:objects (thf/current-page file))
|
|
root (get objects (thi/id :copy-root))
|
|
child-id (first (:shapes root))
|
|
modif-tree (gm/set-objects-modifiers
|
|
{(:id root) {:modifiers (ctm/rotation-modifiers root (gpt/point 50 50) 30)}}
|
|
objects)
|
|
tree (clp/calculate-ignore-tree objects (keys modif-tree) (clp/modifiers-transform modif-tree))]
|
|
(t/testing "the root's rotation is an override"
|
|
(t/is (false? (get tree (:id root)))))
|
|
(t/testing "its child keeps its geometry relative to it"
|
|
(t/is (true? (get tree child-id))))))
|