mirror of
https://github.com/penpot/penpot.git
synced 2026-11-16 15:11:17 +00:00
🐛 Fix integrity error copying variants across files
This commit is contained in:
parent
5e5722dfe7
commit
c466c289de
@ -28,6 +28,25 @@
|
||||
(map #(ctcl/get-component data % true))
|
||||
reverse)))
|
||||
|
||||
(defn collect-variant-properties
|
||||
"Map of component-id → variant-properties for clipboard copy.
|
||||
Variant container: every related component. Variant main: that
|
||||
component only. Anything else: empty map."
|
||||
[data objects shape]
|
||||
(cond
|
||||
(ctc/is-variant-container? shape)
|
||||
(into {}
|
||||
(map (juxt :id :variant-properties))
|
||||
(find-variant-components data objects (:id shape)))
|
||||
|
||||
(ctc/is-variant-main? shape)
|
||||
(if-let [component (ctcl/get-component data (:component-id shape) true)]
|
||||
{(:id component) (:variant-properties component)}
|
||||
{})
|
||||
|
||||
:else
|
||||
{}))
|
||||
|
||||
(defn extract-properties-names
|
||||
[shape data]
|
||||
(->> shape
|
||||
|
||||
@ -2706,7 +2706,8 @@
|
||||
page-id
|
||||
nil
|
||||
variant-id
|
||||
props))]
|
||||
props
|
||||
{:apply-changes-local-library? true}))]
|
||||
[root-shape changes]))
|
||||
|
||||
(defn generate-add-component
|
||||
@ -3218,6 +3219,25 @@
|
||||
(and is-component-root? is-component-main?)
|
||||
(regenerate-component new-obj))
|
||||
|
||||
;; Unknown-component paste into a variant set: align props/names
|
||||
;; after regenerate. Reload the shape so we use the new component-id.
|
||||
;; Prefit the component name/path to the container so
|
||||
;; create-new-properties-from-variant does not treat the component
|
||||
;; full name as a former set name and inflate properties (add-name?).
|
||||
changes (cond-> changes
|
||||
(and is-component-root?
|
||||
is-component-main?
|
||||
(ctk/is-variant-container? parent))
|
||||
(as-> $
|
||||
(let [shape (get (pcb/get-objects $) (:id new-obj))
|
||||
[cpath cname] (cpn/split-group-name (:name parent))
|
||||
changes (pcb/update-component
|
||||
$
|
||||
(:component-id shape)
|
||||
#(assoc % :name cname :path cpath)
|
||||
{:apply-changes-local-library? true})]
|
||||
(clvp/generate-make-shapes-variant changes [shape] parent))))
|
||||
|
||||
;; This is needed for the recursive call to find the new object as parent
|
||||
page' (ctst/add-shape (:id new-obj)
|
||||
new-obj
|
||||
|
||||
@ -247,10 +247,20 @@
|
||||
:name (:name variant-container)))]
|
||||
(reduce
|
||||
(fn [changes shape]
|
||||
(let [component (ctcl/get-component data (:component-id shape) true)]
|
||||
(if (or (zero? num-shapes) ;; do nothing if there are no shapes
|
||||
(and (= variant-id (:variant-id shape)) ;; or we are only moving the shape inside its parent (it is
|
||||
(not (:deleted component)))) ;; the same parent and the component isn't deleted)
|
||||
(let [component (ctcl/get-component data (:component-id shape) true)
|
||||
already-here? (= variant-id (:variant-id shape))
|
||||
prop-names (cfv/extract-properties-names shape data)
|
||||
base-names (map :name base-props)
|
||||
props-ok? (= prop-names base-names)
|
||||
names-ok? (= (cpn/merge-path-item (:path component) (:name component))
|
||||
container-name)
|
||||
variant-ok? (= (:variant-name shape)
|
||||
(ctv/properties-to-name (:variant-properties component)))
|
||||
aligned? (and props-ok? names-ok? variant-ok?)]
|
||||
(if (or (zero? num-shapes) ;; do nothing if there are no shapes
|
||||
(and already-here? ;; or already a fully aligned child of this container
|
||||
aligned?
|
||||
(not (:deleted component))))
|
||||
changes
|
||||
(let [props (create-new-properties shape total-props)
|
||||
variant-name (ctv/properties-to-name props)]
|
||||
|
||||
49
common/test/common_tests/files/variant_test.cljc
Normal file
49
common/test/common_tests/files/variant_test.cljc
Normal file
@ -0,0 +1,49 @@
|
||||
;; 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.variant-test
|
||||
(:require
|
||||
[app.common.files.variant :as cfv]
|
||||
[app.common.test-helpers.components :as thc]
|
||||
[app.common.test-helpers.files :as thf]
|
||||
[app.common.test-helpers.ids-map :as thi]
|
||||
[app.common.test-helpers.shapes :as ths]
|
||||
[app.common.test-helpers.variants :as thv]
|
||||
[clojure.test :as t]))
|
||||
|
||||
(t/use-fixtures :each thi/test-fixture)
|
||||
|
||||
(t/deftest test-collect-variant-properties-for-main
|
||||
(let [file (-> (thf/sample-file :file1)
|
||||
(thv/add-variant :v01 :c01 :m01 :c02 :m02))
|
||||
page (thf/current-page file)
|
||||
main (ths/get-shape file :m01)
|
||||
comp (thc/get-component file :c01)
|
||||
collected (cfv/collect-variant-properties (:data file) (:objects page) main)]
|
||||
|
||||
(t/is (= {(:id comp) (:variant-properties comp)} collected))))
|
||||
|
||||
(t/deftest test-collect-variant-properties-for-container
|
||||
(let [file (-> (thf/sample-file :file1)
|
||||
(thv/add-variant :v01 :c01 :m01 :c02 :m02))
|
||||
page (thf/current-page file)
|
||||
container (ths/get-shape file :v01)
|
||||
c01 (thc/get-component file :c01)
|
||||
c02 (thc/get-component file :c02)
|
||||
collected (cfv/collect-variant-properties (:data file) (:objects page) container)]
|
||||
|
||||
(t/is (= {(:id c01) (:variant-properties c01)
|
||||
(:id c02) (:variant-properties c02)}
|
||||
collected))))
|
||||
|
||||
(t/deftest test-collect-variant-properties-for-plain-shape
|
||||
(let [file (-> (thf/sample-file :file1)
|
||||
(ths/add-sample-shape :rect1 :type :rect))
|
||||
page (thf/current-page file)
|
||||
shape (ths/get-shape file :rect1)
|
||||
collected (cfv/collect-variant-properties (:data file) (:objects page) shape)]
|
||||
|
||||
(t/is (= {} collected))))
|
||||
202
common/test/common_tests/logic/variants_paste_test.cljc
Normal file
202
common/test/common_tests/logic/variants_paste_test.cljc
Normal file
@ -0,0 +1,202 @@
|
||||
;; 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.variants-paste-test
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.files.changes-builder :as pcb]
|
||||
[app.common.files.helpers :as cfh]
|
||||
[app.common.files.validate :as cfv]
|
||||
[app.common.geom.point :as gpt]
|
||||
[app.common.logic.libraries :as cll]
|
||||
[app.common.logic.variant-properties :as clvp]
|
||||
[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.test-helpers.variants :as thv]
|
||||
[app.common.types.component :as ctk]
|
||||
[app.common.types.variant :as ctv]
|
||||
[clojure.test :as t]))
|
||||
|
||||
(t/use-fixtures :each thi/test-fixture)
|
||||
|
||||
(defn- paste-variants-into-container
|
||||
"Paste individual variants from file-a into file-b's variant container.
|
||||
Omit file-a from `libraries` to hit the unknown-component path; include
|
||||
it to hit `duplicate-variant`. Optional `variant-props` mirrors clipboard
|
||||
collection (Task 2)."
|
||||
[file-a file-b source-labels target-container-label
|
||||
& {:keys [libraries variant-props]
|
||||
:or {libraries {(:id file-b) file-b}}}]
|
||||
(let [page-a (thf/current-page file-a)
|
||||
page-b (thf/current-page file-b)
|
||||
container-b (ths/get-shape file-b target-container-label)
|
||||
parent-id (:id container-b)
|
||||
|
||||
source-ids (mapv thi/id source-labels)
|
||||
tree-ids (into []
|
||||
(mapcat #(cfh/get-children-ids-with-self (:objects page-a) %))
|
||||
source-ids)
|
||||
|
||||
pasted (->> tree-ids
|
||||
(map (d/getf (:objects page-a)))
|
||||
(map (fn [shape]
|
||||
(cond-> shape
|
||||
(contains? (set source-ids) (:id shape))
|
||||
(assoc :parent-id parent-id
|
||||
:frame-id parent-id))))
|
||||
(d/index-by :id))
|
||||
|
||||
all-objects (merge (:objects page-b) pasted)]
|
||||
(-> (pcb/empty-changes nil)
|
||||
(pcb/with-page-id (:id page-b))
|
||||
(pcb/with-library-data (:data file-b))
|
||||
(pcb/with-objects all-objects)
|
||||
(cll/generate-duplicate-changes all-objects
|
||||
page-b
|
||||
(set source-ids)
|
||||
(gpt/point 0 0)
|
||||
libraries
|
||||
(:data file-b)
|
||||
(:id file-b)
|
||||
{:variant-props variant-props}))))
|
||||
|
||||
(defn- property-names
|
||||
[component]
|
||||
(mapv :name (:variant-properties component)))
|
||||
|
||||
(defn- variant-mains-in
|
||||
[file container-label]
|
||||
(let [container (ths/get-shape file container-label)]
|
||||
(->> (vals (:objects (thf/current-page file)))
|
||||
(filter ctk/main-instance?)
|
||||
(filter #(= (:id container) (:variant-id %))))))
|
||||
|
||||
(defn- other-set-file
|
||||
[]
|
||||
(-> (thf/sample-file :file-b)
|
||||
(thv/add-variant-two-properties :vb :cb1 :mb1 :cb2 :mb2)
|
||||
(ths/update-shape :vb :name "Other")
|
||||
(ths/update-shape :mb1 :name "Other")
|
||||
(ths/update-shape :mb2 :name "Other")
|
||||
(thc/update-component :cb1 {:name "Other"})
|
||||
(thc/update-component :cb2 {:name "Other"})))
|
||||
|
||||
(t/deftest test-paste-unknown-variant-into-container-validates
|
||||
;; #10704: paste individual variants from file A into file B's set
|
||||
;; when A is not in libraries must leave a valid file (desired behavior).
|
||||
(let [file-a (-> (thf/sample-file :file-a)
|
||||
(thv/add-variant :va :ca1 :ma1 :ca2 :ma2))
|
||||
file-b (other-set-file)
|
||||
changes (paste-variants-into-container file-a file-b [:ma1 :ma2] :vb)
|
||||
file' (thf/apply-changes file-b changes :validate? false)
|
||||
errors (cfv/validate-file file' {})
|
||||
pasted (->> (variant-mains-in file' :vb)
|
||||
(remove #(#{(thi/id :mb1) (thi/id :mb2)} (:id %))))]
|
||||
|
||||
(t/is (nil? errors))
|
||||
(doseq [shape pasted]
|
||||
(let [comp (thc/get-component-by-id file' (:component-id shape))]
|
||||
(t/is (= ["Property 1" "Property 2"] (property-names comp)))
|
||||
(t/is (= "Other" (:name shape)))
|
||||
(t/is (= (:variant-name shape)
|
||||
(ctv/properties-to-name (:variant-properties comp))))))))
|
||||
|
||||
(t/deftest test-duplicate-variant-in-own-container-bumps-last-value
|
||||
;; Same-container duplicate via duplicate-variant: originals keep their
|
||||
;; props; the new main gets a bumped last-property value. Does not cover
|
||||
;; generate-make-shapes-variant's already-aligned early-exit (new mains
|
||||
;; enter with a nil variant-id).
|
||||
(let [file (-> (thf/sample-file :file1)
|
||||
(thv/add-variant :v01 :c01 :m01 :c02 :m02))
|
||||
c01-props (:variant-properties (thc/get-component file :c01))
|
||||
c02-props (:variant-properties (thc/get-component file :c02))
|
||||
file' (tho/duplicate-shape file :m01)
|
||||
mains (variant-mains-in file' :v01)
|
||||
new-main (->> mains
|
||||
(remove #(#{(thi/id :m01) (thi/id :m02)} (:id %)))
|
||||
first)
|
||||
new-comp (thc/get-component-by-id file' (:component-id new-main))]
|
||||
|
||||
(thf/validate-file! file')
|
||||
(t/is (= 3 (count mains)))
|
||||
(t/is (= c01-props (:variant-properties (thc/get-component file' :c01))))
|
||||
(t/is (= c02-props (:variant-properties (thc/get-component file' :c02))))
|
||||
(t/is (= ["Property 1"] (property-names new-comp)))
|
||||
(t/is (= (str ctv/value-prefix 3)
|
||||
(-> new-comp :variant-properties last :value)))))
|
||||
|
||||
(t/deftest test-make-shapes-variant-skips-when-already-aligned
|
||||
;; Locks the narrowed early-exit: a fully aligned child must not have its
|
||||
;; props or variant-name rewritten.
|
||||
(let [file (-> (thf/sample-file :file1)
|
||||
(thv/add-variant :v01 :c01 :m01 :c02 :m02))
|
||||
page (thf/current-page file)
|
||||
container (ths/get-shape file :v01)
|
||||
shape (ths/get-shape file :m01)
|
||||
before (:variant-properties (thc/get-component file :c01))
|
||||
before-vn (:variant-name shape)
|
||||
changes (-> (pcb/empty-changes nil)
|
||||
(pcb/with-page page)
|
||||
(pcb/with-objects (:objects page))
|
||||
(pcb/with-library-data (:data file))
|
||||
(clvp/generate-make-shapes-variant [shape] container))
|
||||
file' (thf/apply-changes file changes)
|
||||
shape' (ths/get-shape-by-id file' (:id shape))]
|
||||
|
||||
(t/is (= before (:variant-properties (thc/get-component file' :c01))))
|
||||
(t/is (= before-vn (:variant-name shape')))))
|
||||
|
||||
(t/deftest test-paste-known-variant-into-other-container-aligns
|
||||
;; file A present in libraries → duplicate-variant path; props must match
|
||||
;; the target set and the file must validate.
|
||||
(let [file-a (-> (thf/sample-file :file-a)
|
||||
(thv/add-variant :va :ca1 :ma1 :ca2 :ma2))
|
||||
file-b (other-set-file)
|
||||
libraries {(:id file-a) file-a
|
||||
(:id file-b) file-b}
|
||||
changes (paste-variants-into-container file-a file-b [:ma1 :ma2] :vb
|
||||
:libraries libraries)
|
||||
file' (thf/apply-changes file-b changes :validate? false)
|
||||
errors (cfv/validate-file file' {})
|
||||
pasted (->> (variant-mains-in file' :vb)
|
||||
(remove #(#{(thi/id :mb1) (thi/id :mb2)} (:id %))))]
|
||||
|
||||
(t/is (nil? errors))
|
||||
(t/is (= 2 (count pasted)))
|
||||
(doseq [shape pasted]
|
||||
(let [comp (thc/get-component-by-id file' (:component-id shape))]
|
||||
(t/is (= ["Property 1" "Property 2"] (property-names comp)))
|
||||
(t/is (= "Other" (:name shape)))
|
||||
(t/is (= (:variant-name shape)
|
||||
(ctv/properties-to-name (:variant-properties comp))))))))
|
||||
|
||||
(t/deftest test-paste-unknown-preserves-matching-property-values
|
||||
;; Clipboard props (Task 2) whose names exist on the target survive regenerate.
|
||||
(let [file-a (-> (thf/sample-file :file-a)
|
||||
(thv/add-variant :va :ca1 :ma1 :ca2 :ma2))
|
||||
file-b (other-set-file)
|
||||
props {(:id (thc/get-component file-a :ca1))
|
||||
(:variant-properties (thc/get-component file-a :ca1))
|
||||
(:id (thc/get-component file-a :ca2))
|
||||
(:variant-properties (thc/get-component file-a :ca2))}
|
||||
changes (paste-variants-into-container file-a file-b [:ma1 :ma2] :vb
|
||||
:variant-props props)
|
||||
file' (thf/apply-changes file-b changes :validate? false)
|
||||
pasted (->> (variant-mains-in file' :vb)
|
||||
(remove #(#{(thi/id :mb1) (thi/id :mb2)} (:id %)))
|
||||
(map #(thc/get-component-by-id file' (:component-id %))))
|
||||
values (->> pasted
|
||||
(mapcat :variant-properties)
|
||||
(filter #(= "Property 1" (:name %)))
|
||||
(map :value)
|
||||
set)]
|
||||
|
||||
(thf/validate-file! file')
|
||||
(t/is (contains? values "Value1"))
|
||||
(t/is (contains? values "Value2"))))
|
||||
@ -30,6 +30,7 @@
|
||||
[common-tests.files.shapes-builder-test]
|
||||
[common-tests.files.tokens-test]
|
||||
[common-tests.files.validate-test]
|
||||
[common-tests.files.variant-test]
|
||||
[common-tests.geom-align-test]
|
||||
[common-tests.geom-bounds-layout-nil-test]
|
||||
[common-tests.geom-bounds-map-test]
|
||||
@ -79,6 +80,7 @@
|
||||
[common-tests.logic.text-touched-test]
|
||||
[common-tests.logic.token-apply-test]
|
||||
[common-tests.logic.token-test]
|
||||
[common-tests.logic.variants-paste-test]
|
||||
[common-tests.logic.variants-reparent-test]
|
||||
[common-tests.logic.variants-switch-test]
|
||||
[common-tests.logic.variants-test]
|
||||
@ -140,6 +142,7 @@
|
||||
'common-tests.files.shapes-builder-test
|
||||
'common-tests.files.tokens-test
|
||||
'common-tests.files.validate-test
|
||||
'common-tests.files.variant-test
|
||||
'common-tests.geom-align-test
|
||||
'common-tests.geom-bounds-layout-nil-test
|
||||
'common-tests.geom-bounds-map-test
|
||||
@ -189,6 +192,7 @@
|
||||
'common-tests.logic.text-touched-test
|
||||
'common-tests.logic.token-apply-test
|
||||
'common-tests.logic.token-test
|
||||
'common-tests.logic.variants-paste-test
|
||||
'common-tests.logic.variants-reparent-test
|
||||
'common-tests.logic.variants-switch-test
|
||||
'common-tests.logic.variants-test
|
||||
|
||||
@ -157,10 +157,8 @@
|
||||
(let [page-id (:current-page-id state)
|
||||
data (dsh/lookup-file-data state)
|
||||
objects (-> (dsh/get-page data page-id)
|
||||
(get :objects))
|
||||
|
||||
components (cfv/find-variant-components data objects (:id shape))]
|
||||
(into {} (map (juxt :id :variant-properties) components))))
|
||||
(get :objects))]
|
||||
(cfv/collect-variant-properties data objects shape)))
|
||||
|
||||
|
||||
;; Collects all the items together and split images into a
|
||||
@ -176,7 +174,8 @@
|
||||
(some? images)
|
||||
(update :images into images)
|
||||
|
||||
(ctk/is-variant-container? item)
|
||||
(or (ctk/is-variant-container? item)
|
||||
(ctk/is-variant-main? item))
|
||||
(update :variant-properties merge (collect-variants state item))))
|
||||
|
||||
(maybe-translate [shape objects parent-frame-id]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user