From c9c9a574a00f911d401acde0f014b7be6b056430 Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Fri, 20 Nov 2026 15:35:40 +0100 Subject: [PATCH] :bug: Repair variant property set when removing a property (#12960) * :bug: Repair variant property set when removing a property Removing a property could leave two variants with the same value combination. The remaining property vectors now go through ctv/sanitize-properties-set, and each main's variant name is refreshed, so the set stays valid, as the 0038 migration leaves it. On a clash, the variant higher in the layers panel keeps its values and the lower one gets a " (n)" suffix. The plugin docs for removeProperty say so. The rename-in-layers path removes properties that are empty in every variant. That cannot make two variants equal, so it skips the clean-up: a duplicate made by the rename stays and its warning shows. Closes #12959 AI-assisted-by: claude-opus-5-5 * :bug: Keep the top variant on a clash in migration 0038 Migration 0038 cleaned the members of a set in :shapes order, so on a value clash the bottom variant in the layers panel kept its values and the top one got a " (n)" suffix. The top variant is the primary one, and removing a property already keeps it. update-variant-container-properties now passes the members top first, so both paths agree: the primary variant keeps its values and its property names lead the set. 0038 is not in a release yet, so it changes in place. The removeProperty plugin docs now cover three or more clashing variants and all-blank values, and the plugins CHANGELOG has an entry for the fix. AI-assisted-by: claude-opus-5-5 --- common/src/app/common/files/migrations.cljc | 3 +- common/src/app/common/files/variant.cljc | 10 ++- .../app/common/logic/variant_properties.cljc | 37 ++++++---- common/src/app/common/types/variant.cljc | 5 +- .../test/common_tests/files/variant_test.cljc | 11 +-- .../files_migrations_0038_test.cljc | 38 +++++++--- .../common_tests/logic/variants_test.cljc | 71 ++++++++++++++++++- .../src/app/main/data/workspace/variants.cljs | 5 +- .../data/workspace_variants_test.cljs | 27 +++++++ plugins/CHANGELOG.md | 1 + plugins/libs/plugin-types/index.d.ts | 5 +- 11 files changed, 177 insertions(+), 36 deletions(-) diff --git a/common/src/app/common/files/migrations.cljc b/common/src/app/common/files/migrations.cljc index b3d71465db..1c7b8d82bb 100644 --- a/common/src/app/common/files/migrations.cljc +++ b/common/src/app/common/files/migrations.cljc @@ -2450,7 +2450,8 @@ ;; editor treats those as separators, and a later edit strips them. ;; Files saved before that strip can also hold two identical names, two ;; identical value combinations, or a label that was not saved with the -;; values. Rewrite the text in place and refresh the label. +;; values. Rewrite the text in place and refresh the label. On a clash, +;; the variant at the top of the layers panel keeps its values. (defn- variant-container-entries [data] (concat diff --git a/common/src/app/common/files/variant.cljc b/common/src/app/common/files/variant.cljc index 01baabbfbe..e4fcfcae6d 100644 --- a/common/src/app/common/files/variant.cljc +++ b/common/src/app/common/files/variant.cljc @@ -170,10 +170,14 @@ (defn update-variant-container-properties "Apply `f` to the property vectors of the members of `container`, in - `:shapes` order, and write each result with `update-variant-properties`. - `f` takes and returns a vector of property vectors." + layers-panel order (top first, the reverse of `:shapes`), and write each + result with `update-variant-properties`. `f` takes and returns a vector + of property vectors." [data objects container f] - (let [components (mapv :component (qualifying-variant-members data objects container)) + (let [components (->> (qualifying-variant-members data objects container) + (map :component) + (reverse) + (vec)) props-seq (f (mapv #(vec (or (:variant-properties %) [])) components))] (reduce (fn [data [component props]] (update-variant-properties data component (constantly props))) diff --git a/common/src/app/common/logic/variant_properties.cljc b/common/src/app/common/logic/variant_properties.cljc index 141cbd2279..7938cdbd06 100644 --- a/common/src/app/common/logic/variant_properties.cljc +++ b/common/src/app/common/logic/variant_properties.cljc @@ -37,23 +37,32 @@ (defn generate-remove-property - [changes variant-id pos] - (let [data (pcb/get-library-data changes) - objects (pcb/get-objects changes) - related-components (cfv/find-variant-components data objects variant-id) - props (-> related-components first :variant-properties)] + "Remove the property at `pos` from every variant of the set. Then, unless + `sanitize-set?` is false, sanitize the remaining set as a whole, so two + variants that differed only in the removed property do not end up with + the same values." + [changes variant-id pos & {:keys [sanitize-set?] :or {sanitize-set? true}}] + (let [data (pcb/get-library-data changes) + objects (pcb/get-objects changes) + ;; Layers-panel order, the reverse of :shapes: on a clash the variant + ;; higher in the panel keeps its values and the lower one gets " (n)". + components (->> (cfv/qualifying-variant-members data objects (get objects variant-id)) + (map :component) + (reverse)) + props (-> components first :variant-properties)] (if (and (seq props) (<= 0 pos) (< pos (count props))) - (reduce (fn [changes component] - (let [props (:variant-properties component) - props (d/remove-at-index props pos) - main-id (:main-instance-id component) - name (ctv/properties-to-name props)] + (let [stripped (mapv #(d/remove-at-index (:variant-properties %) pos) components) + props-seq (cond-> stripped + sanitize-set? ctv/sanitize-properties-set)] + (reduce (fn [changes [component props]] (-> changes - (pcb/update-component (:id component) #(assoc % :variant-properties props) + (pcb/update-component (:id component) + #(assoc % :variant-properties props) {:apply-changes-local-library? true}) - (pcb/update-shapes [main-id] #(assoc % :variant-name name))))) - changes - related-components) + (pcb/update-shapes [(:main-instance-id component)] + #(assoc % :variant-name (ctv/properties-to-name props))))) + changes + (map vector components props-seq))) changes))) diff --git a/common/src/app/common/types/variant.cljc b/common/src/app/common/types/variant.cljc index f4a6636448..50200e5e74 100644 --- a/common/src/app/common/types/variant.cljc +++ b/common/src/app/common/types/variant.cljc @@ -343,8 +343,9 @@ (defn sanitize-properties-set "Sanitize the property vectors of all the variants of one set, given in - board order. Every vector gets the same names in the same order, and no - two vectors share a value combination. Earlier vectors keep their values." + layers-panel order (top first). Every vector gets the same names in the + same order, and no two vectors share a value combination. Earlier + vectors keep their values." [props-seq] (let [cleaned (mapv sanitize-properties props-seq) names (properties-names cleaned)] diff --git a/common/test/common_tests/files/variant_test.cljc b/common/test/common_tests/files/variant_test.cljc index 15b7386ab2..93dccc0934 100644 --- a/common/test/common_tests/files/variant_test.cljc +++ b/common/test/common_tests/files/variant_test.cljc @@ -91,8 +91,9 @@ (t/is (= "big" (get-in data' [:components deleted-id :objects main-id :variant-name]))))) -(t/deftest test-sanitize-variant-container-cleans-members-in-board-order - ;; :shapes of the board is [m02 m01], so c02 keeps its value. +(t/deftest test-sanitize-variant-container-cleans-members-in-layers-panel-order + ;; :shapes of the board is [m02 m01], so m01 is the top variant in the + ;; layers panel and c01 keeps its value. (let [props [{:name "Property 1" :value "a=b"}] file (-> (variant-file) (thc/update-component :c01 {:variant-properties props}) @@ -103,10 +104,10 @@ file' (assoc file :data data')] (t/is (= [{:name "Property 1" :value "ab"}] - (:variant-properties (thc/get-component file' :c02)))) - (t/is (= [{:name "Property 1" :value "ab (1)"}] (:variant-properties (thc/get-component file' :c01)))) - (t/is (= "ab (1)" (:variant-name (ths/get-shape file' :m01)))))) + (t/is (= [{:name "Property 1" :value "ab (1)"}] + (:variant-properties (thc/get-component file' :c02)))) + (t/is (= "ab (1)" (:variant-name (ths/get-shape file' :m02)))))) (t/deftest test-sanitize-variant-component (let [file (-> (variant-file) diff --git a/common/test/common_tests/files_migrations_0038_test.cljc b/common/test/common_tests/files_migrations_0038_test.cljc index d5ea3383d4..850c6e8468 100644 --- a/common/test/common_tests/files_migrations_0038_test.cljc +++ b/common/test/common_tests/files_migrations_0038_test.cljc @@ -42,8 +42,13 @@ (-> (thf/sample-file :file1) (thv/add-variant :variant1 :component1 :root1 :component2 :root2))) +(defn- prop-named + [props name] + (some #(when (= name (:name %)) %) props)) + (t/deftest migration-0038-strips-forbidden-characters - ;; root2 is the first child of :shapes, so its names lead the set. + ;; root1 is the last child of :shapes (top of the layers panel), so its + ;; names lead the set. (let [file (-> (variant-file) (thk/update-component :component2 {:variant-properties [{:name "Size=Big" :value "a=b," @@ -58,13 +63,11 @@ copy' (ths/get-shape file' :copy2) data' (ctf/file-data file')] - (t/is (= "SizeBig" (-> comp2' :variant-properties first :name))) - (t/is (= "ab" (-> comp2' :variant-properties first :value))) - (t/is (true? (-> comp2' :variant-properties first :hidden))) + (t/is (= ["Property 1" "SizeBig"] (mapv :name (:variant-properties comp2')))) + (t/is (= "ab" (:value (prop-named (:variant-properties comp2') "SizeBig")))) + (t/is (true? (:hidden (prop-named (:variant-properties comp2') "SizeBig")))) (t/is (= "ab" (:variant-name root2'))) - (t/is (= "Value1" (->> (:variant-properties comp1') - (some #(when (= "Property 1" (:name %)) %)) - :value))) + (t/is (= "Value1" (:value (prop-named (:variant-properties comp1') "Property 1")))) (t/is (= copy copy') "copies are left untouched") (t/is (= data' (cfm/migrate-data data' migration-id))))) @@ -86,6 +89,25 @@ (t/is (= (ctv/properties-to-name props) (:variant-name root'))) (t/is (= data' (cfm/migrate-data data' migration-id))))) +(t/deftest migration-0038-keeps-the-top-variant-on-a-clash + ;; root1 is the last child of :shapes, so it is the top variant in the + ;; layers panel and the primary one: it keeps its values. + (let [props [{:name "Size" :value "Big"}] + file (-> (variant-file) + (thk/update-component :component1 {:variant-properties props}) + (thk/update-component :component2 {:variant-properties props}) + (ths/update-shape :root1 :variant-name "Big") + (ths/update-shape :root2 :variant-name "Big")) + file' (migrate-text file) + data' (ctf/file-data file')] + + (t/is (= props (:variant-properties (thk/get-component file' :component1)))) + (t/is (= "Big" (:variant-name (ths/get-shape file' :root1)))) + (t/is (= [{:name "Size" :value "Big (1)"}] + (:variant-properties (thk/get-component file' :component2)))) + (t/is (= "Big (1)" (:variant-name (ths/get-shape file' :root2)))) + (t/is (= data' (cfm/migrate-data data' migration-id))))) + (t/deftest migration-0038-does-not-take-names-from-a-detached-component (let [deleted-id (uuid/next) main-id (uuid/next) @@ -133,5 +155,5 @@ file' (cfm/migrate-file file {})] (t/is (contains? (:migrations file') migration-id)) - (t/is (= "SizeBig" (-> (thk/get-component file' :component2) :variant-properties first :name))) + (t/is (some? (prop-named (:variant-properties (thk/get-component file' :component2)) "SizeBig"))) (t/is (some? (ctf/check-file-data (:data file')))))) diff --git a/common/test/common_tests/logic/variants_test.cljc b/common/test/common_tests/logic/variants_test.cljc index a2ba965283..42cfe2fbb1 100644 --- a/common/test/common_tests/logic/variants_test.cljc +++ b/common/test/common_tests/logic/variants_test.cljc @@ -211,7 +211,76 @@ (t/is (= (count (:variant-properties comp01')) 1)) (t/is (= (count (:variant-properties comp02)) 2)) (t/is (= (count (:variant-properties comp02')) 1)) - (t/is (= (-> comp01' :variant-properties first :name) "Property 2")))) + (t/is (= (-> comp01' :variant-properties first :name) "Property 2")) + ;; :m01 is higher in the layers panel, so it keeps its value. + (t/is (= "" (-> comp01' :variant-properties first :value))) + (t/is (= "(1)" (-> comp02' :variant-properties first :value))) + (t/is (= "" (:variant-name (ths/get-shape file' :m01)))) + (t/is (= "(1)" (:variant-name (ths/get-shape file' :m02)))))) + +(defn- size-color-shape + [size color shape] + [{:name "Size" :value size} + {:name "Color" :value color} + {:name "Shape" :value shape}]) + +(defn- variant-set-file + "Two variants; :m01 is higher in the layers panel than :m02." + [props1 props2] + (-> (thf/sample-file :file1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (thc/update-component :c01 {:variant-properties props1}) + (thc/update-component :c02 {:variant-properties props2}) + (ths/update-shape :m01 :variant-name (ctv/properties-to-name props1)) + (ths/update-shape :m02 :variant-name (ctv/properties-to-name props2)))) + +(defn- remove-property-changes + [file pos & {:keys [sanitize-set?] :or {sanitize-set? true}}] + (let [page (thf/current-page file)] + (-> (pcb/empty-changes nil) + (pcb/with-page-id (:id page)) + (pcb/with-library-data (:data file)) + (pcb/with-objects (:objects page)) + (clvp/generate-remove-property (thi/id :v01) pos :sanitize-set? sanitize-set?)))) + +(defn- values + [file component-label] + (mapv :value (:variant-properties (thc/get-component file component-label)))) + +(t/deftest test-remove-property-numbers-the-lower-variant-on-a-clash + (let [file (variant-set-file (size-color-shape "Big" "red" "round") + (size-color-shape "Big" "blue" "round")) + file' (thf/apply-changes file (remove-property-changes file 1))] + + (t/is (= ["Big" "round"] (values file' :c01))) + (t/is (= ["Big" "round (1)"] (values file' :c02))) + (t/is (= "Big, round" (:variant-name (ths/get-shape file' :m01)))) + (t/is (= "Big, round (1)" (:variant-name (ths/get-shape file' :m02)))))) + +(t/deftest test-remove-property-without-a-clash-keeps-the-values + (let [file (variant-set-file (size-color-shape "Big" "red" "round") + (size-color-shape "Small" "blue" "square")) + file' (thf/apply-changes file (remove-property-changes file 1))] + + (t/is (= ["Big" "round"] (values file' :c01))) + (t/is (= ["Small" "square"] (values file' :c02))) + (t/is (= "Small, square" (:variant-name (ths/get-shape file' :m02)))))) + +(t/deftest test-remove-property-without-sanitize-keeps-a-duplicate + (let [file (variant-set-file (size-color-shape "Big" "red" "round") + (size-color-shape "Big" "blue" "round")) + file' (thf/apply-changes file (remove-property-changes file 1 :sanitize-set? false))] + + (t/is (= ["Big" "round"] (values file' :c01))) + (t/is (= ["Big" "round"] (values file' :c02))) + (t/is (= "Big, round" (:variant-name (ths/get-shape file' :m02)))))) + +(t/deftest test-remove-property-out-of-range-does-nothing + (let [file (variant-set-file (size-color-shape "Big" "red" "round") + (size-color-shape "Big" "blue" "round"))] + + (t/is (empty? (:redo-changes (remove-property-changes file -1)))) + (t/is (empty? (:redo-changes (remove-property-changes file 3)))))) (t/deftest test-update-property-value (let [;; ==== Setup diff --git a/frontend/src/app/main/data/workspace/variants.cljs b/frontend/src/app/main/data/workspace/variants.cljs index 8c7cdbe34c..eb9b08e349 100644 --- a/frontend/src/app/main/data/workspace/variants.cljs +++ b/frontend/src/app/main/data/workspace/variants.cljs @@ -249,7 +249,10 @@ (fn [changes [pos property-empty?]] (if property-empty? (-> changes - (clvp/generate-remove-property variant-id pos)) + ;; Removing a property that is empty everywhere + ;; cannot make two variants equal: keep any + ;; duplicate the rename made, so its warning shows. + (clvp/generate-remove-property variant-id pos :sanitize-set? false)) changes)) changes properties-empty-pos) diff --git a/frontend/test/frontend_tests/data/workspace_variants_test.cljs b/frontend/test/frontend_tests/data/workspace_variants_test.cljs index b293ba2560..0c80611d03 100644 --- a/frontend/test/frontend_tests/data/workspace_variants_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_variants_test.cljs @@ -162,6 +162,33 @@ (t/is (= expected names2)) (t/is (not-any? #(= "Property 5" %) names1)))))))) +(t/deftest remove-empty-properties-keeps-a-duplicate + ;; A rename made both variants "Big, red"; removing the empty property + ;; must not renumber either one, so the duplicate warning still shows. + (t/async + done + (let [props [{:name "Size" :value "Big"} + {:name "Color" :value "red"} + {:name "Extra" :value ""}] + file (-> (cthf/sample-file :file1 :page-label :page1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (cthc/update-component :c01 {:variant-properties props}) + (cthc/update-component :c02 {:variant-properties props}) + (cths/update-shape :m01 :variant-name (ctv/properties-to-name props)) + (cths/update-shape :m02 :variant-name (ctv/properties-to-name props))) + variant-id (cthi/id :v01)] + (run file + [(dwv/remove-empty-properties variant-id)] + done + (fn [state] + (let [file (ths/get-file-from-state state) + values1 (mapv :value (:variant-properties (cthc/get-component file :c01))) + values2 (mapv :value (:variant-properties (cthc/get-component file :c02)))] + (t/is (= ["Big" "red"] values1)) + (t/is (= ["Big" "red"] values2)) + (t/is (= "Big, red" (variant-name file :m01))) + (t/is (= "Big, red" (variant-name file :m02))))))))) + (t/deftest update-property-name-from-another-page (t/async done diff --git a/plugins/CHANGELOG.md b/plugins/CHANGELOG.md index 3e51bb5ff8..550c2ee12f 100644 --- a/plugins/CHANGELOG.md +++ b/plugins/CHANGELOG.md @@ -22,6 +22,7 @@ - **plugins-runtime:** `LibraryComponent.mainInstance()` returns `null` for a deleted component, instead of a shape with an empty `id` whose `parent` throws. - **plugins-runtime**: `TokenFontFamilies.resolvedValue` and the `fontFamilies` member of `TokenTypographyValue` now keep a family whose name has several words (`Hanken Grotesk`) as one entry, instead of splitting it into an array of its words, and keep names with words the resolver would otherwise read as colors or numbers (`Red Hat Display`, `Press Start 2P`) unchanged. - **plugins-runtime:** A SES `repairIntrinsics` failure at startup (for example, when a browser extension wraps the native `Temporal` global) no longer leaves the whole app blank. Plugins are skipped for that session, the error is logged, and a telemetry event reports the unavailable runtime; the editor, login and sign-up pages load as usual. Plugins work as before in every other environment. When startup fails, SES has already replaced `Function.prototype.toString` in the host realm before throwing, but no intrinsics are hardened and no sandbox is created. Closes #12924. +- **plugins-runtime:** `Variants.removeProperty()` no longer leaves two variants with the same values. When several variants clash, the top one in the layers panel keeps its values and each lower one gets a numbered value (`Big (1)`, or `(1)` when all its remaining values are empty). Closes #12959. ## 1.6.0 (2026-11-10) diff --git a/plugins/libs/plugin-types/index.d.ts b/plugins/libs/plugin-types/index.d.ts index 3ae76ca542..05a2d3faee 100644 --- a/plugins/libs/plugin-types/index.d.ts +++ b/plugins/libs/plugin-types/index.d.ts @@ -6171,7 +6171,10 @@ export interface Variants { currentValues(property: string): string[]; /** - * Remove a property of the Variant + * Remove a property of the Variant. If several variants end up with the + * same values, the top one in the layers panel keeps them and each lower + * one gets a numbered value: a " (n)" suffix, or "(n)" when all its + * remaining values are empty. * @param pos The position of the property to remove */ removeProperty(pos: number): void;