🐛 Repair variant property set when removing a property (#12960)

* 🐛 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

* 🐛 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
This commit is contained in:
Pablo Alba 2026-11-20 15:35:40 +01:00 committed by GitHub
parent fae248bb35
commit c9c9a574a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 177 additions and 36 deletions

View File

@ -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

View File

@ -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)))

View File

@ -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)))

View File

@ -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)]

View File

@ -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)

View File

@ -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'))))))

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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;