🐛 Fix batch edit of colors in the selected colors menu (#13087)

* 🐛 Fix batch edit of colors in the selected colors menu

Restore the colorpicker flag that commit 20d2d22 lost when it replaced
the flag with the row index. Every change was treated as a picker change,
so the colors already applied were kept and reused by the next edit. That
made an edit on one row change the first color instead.

Direct edits of the color and opacity inputs now target the exact color
of the row. Picker changes keep the previous-color fallback needed while
the selected shapes are still updating.

Add unit tests for the new operation resolver.

AI-assisted-by: deepseek-v4.1-flash

* ♻️ Remove unused picker history from token detachment

Resolve token detachment operations directly from the current groups.

Picker color history is not needed for token detachment.

AI-assisted-by: gpt-6-luna

* ♻️ Clarify color row change arguments

Bind the row color once and name the change arguments.

Treat the picker flag as optional for direct edits.

AI-assisted-by: gpt-6-luna

* 🐛 Fix linting issue
This commit is contained in:
Luis de Dios 2026-11-23 13:01:35 +01:00 committed by GitHub
parent 8fe1b9c659
commit 340f5e36cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 99 additions and 30 deletions

View File

@ -99,17 +99,25 @@
(def xf:map-shape-id
(map :shape-id))
(defn- retrieve-color-operations
[groups old-color prev-colors]
(let [old-color (-> old-color
(dissoc :name :path)
(d/without-nils))
prev-color (d/seek (partial get groups) prev-colors)
color-operations-old (get groups old-color)
color-operations-prev (get groups prev-color)
color-operations (or color-operations-prev color-operations-old)
old-color (or prev-color old-color)]
[color-operations old-color]))
(defn resolve-color-operations
"Resolves the operations of the shapes that a color change must update.
Direct edits (the color and opacity inputs) always target the exact color
shown in the edited row. The colorpicker can invoke `on-change` with a stale
color while the previous change is still propagating to every shape, so for
picker changes it falls back to the most recent color that is already present
on the selected shapes."
[groups old-color prev-colors from-picker?]
(let [old-color (-> old-color
(dissoc :name :path)
(d/without-nils))]
(if from-picker?
(let [prev-color (d/seek (partial get groups) prev-colors)
color-operations-old (get groups old-color)
color-operations-prev (get groups prev-color)
color-operations (or color-operations-prev color-operations-old)]
[color-operations (or prev-color old-color)])
[(get groups old-color) old-color])))
(mf/defc color-selection-menu*
{::mf/wrap [#(mf/memo' % (mf/check-props ["shapes"]))]}
@ -138,7 +146,6 @@
expand-color (mf/use-state false)
expand-token-color (mf/use-state false)
;; TODO: Review if this is still necessary.
prev-colors-ref (mf/use-ref nil)
;; Always keep this ref pointing to the latest groups so that on-change
@ -152,9 +159,12 @@
(fn [old-color new-color from-picker?]
(let [groups (mf/ref-val groups-ref)
prev-colors (mf/ref-val prev-colors-ref)
[color-operations old-color] (retrieve-color-operations groups old-color prev-colors)]
[color-operations old-color] (resolve-color-operations
groups old-color prev-colors from-picker?)]
;; TODO: Review if this is still necessary.
;; The colorpicker can deliver changes faster than they propagate
;; to every selected shape, so keep the applied colors to fall
;; back to them on the next change.
(when from-picker?
(let [color (-> new-color
(dissoc :name :path)
@ -164,6 +174,11 @@
(st/emit! (dwc/change-color-in-selected color-operations new-color (dissoc old-color :token-name :has-token-applied))))))
on-change-for-color
(fn [color]
(fn [new-color _index & [from-picker?]]
(on-change color new-color from-picker?)))
on-open
(mf/use-fn #(mf/set-ref-val! prev-colors-ref []))
@ -182,19 +197,12 @@
(mf/use-fn
(mf/deps token-colors groups)
(fn [token-name]
(let [prev-colors (mf/ref-val prev-colors-ref)
token-color (some #(when (= (:token-name %) token-name) %) token-colors)
[color-operations _] (retrieve-color-operations groups token-color prev-colors)]
(let [token-color (some #(when (= (:token-name %) token-name) %) token-colors)
color-operations (get groups token-color)]
(doseq [op color-operations]
(let [attr (if (= (:prop op) :stroke)
#{:stroke-color}
#{:fill})
color (-> token-color
(dissoc :token-name :has-token-applied)
(d/without-nils))]
(mf/set-ref-val! prev-colors-ref
(conj prev-colors color))
#{:fill})]
(st/emit! (dwta/unapply-token {:token-name token-name
:attributes attr
:shape-ids [(:shape-id op)]})))))))
@ -217,7 +225,8 @@
color (-> new-color
(dissoc :name :path)
(d/without-nils))
[color-operations _] (retrieve-color-operations groups old-color prev-colors)]
[color-operations _] (resolve-color-operations
groups old-color prev-colors false)]
(mf/set-ref-val! prev-colors-ref
(conj prev-colors color))
(st/emit! (dwta/apply-token-on-color-selected color-operations token)))))]
@ -242,7 +251,7 @@
:index index
:on-detach #(on-detach color %)
:select-only select-only
:on-change #(on-change color %1 %2)
:on-change (on-change-for-color color)
:on-token-change #(on-token-change %1 %2 color)
:on-open on-open
:origin :color-selection
@ -260,7 +269,7 @@
:color color
:index index
:select-only select-only
:on-change #(on-change color %1 %2)
:on-change (on-change-for-color color)
:origin :color-selection
:on-token-change #(on-token-change %1 %2 color)
:on-open on-open
@ -281,7 +290,7 @@
:color color
:index index
:select-only select-only
:on-change #(on-change token-color %1 %2)
:on-change (on-change-for-color token-color)
:origin :color-selection
:applied-token (:token-name token-color)
:on-detach-token on-detach-token

View File

@ -302,8 +302,9 @@
:disable-gradient disable-gradient
:disable-opacity disable-opacity
:disable-image disable-image
;; on-change second parameter means if the source is the color-picker
:on-change #(on-change % index)
;; The third argument flags changes coming from the color picker, so
;; callers can tell them apart from direct edits of the color inputs.
:on-change #(on-change % index true)
:on-token-change on-token-change
:on-close (fn [value opacity id file-id]
(when on-close

View File

@ -167,6 +167,7 @@
[frontend-tests.tokens.typography-form-test]
[frontend-tests.tokens.workspace-tokens-remap-test]
[frontend-tests.ui.check-updates-test]
[frontend-tests.ui.color-selection-test]
[frontend-tests.ui.color-token-position-test]
[frontend-tests.ui.colorpicker-token-set-order-test]
[frontend-tests.ui.comment-input-ime-test]
@ -407,6 +408,7 @@
'frontend-tests.tokens.logic.tokens-status-test
'frontend-tests.tokens.workspace-tokens-remap-test
'frontend-tests.ui.check-updates-test
'frontend-tests.ui.color-selection-test
'frontend-tests.ui.color-token-position-test
'frontend-tests.ui.colorpicker-token-set-order-test
'frontend-tests.ui.comment-input-ime-test

View File

@ -0,0 +1,57 @@
;; 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 frontend-tests.ui.color-selection-test
(:require
[app.main.ui.workspace.sidebar.options.menus.color-selection :as color-selection]
[clojure.test :as t :include-macros true]))
(def ^:private color-a
{:color "#f9f06b" :opacity 1})
(def ^:private color-b
{:color "#f8e45c" :opacity 1})
(def ^:private color-c
{:color "#f6d32d" :opacity 1})
(def ^:private groups
{color-a [{:prop :fill :shape-id :shape-a :index 0}]
color-b [{:prop :fill :shape-id :shape-b :index 0}]
color-c [{:prop :fill :shape-id :shape-c :index 0}]})
(t/deftest direct-edit-targets-the-edited-row-color
;; Regression for https://github.com/penpot/penpot/issues/12759: after
;; editing one row, its color stays cached and a direct edit on another row
;; must not reuse those operations.
(let [prev-colors [color-a]]
(t/is (= [(get groups color-b) color-b]
(color-selection/resolve-color-operations
groups color-b prev-colors false)))))
(t/deftest direct-edit-ignores-cached-picker-colors
(let [stale-color (assoc color-a :opacity 0.5)]
(t/is (= [(get groups color-c) color-c]
(color-selection/resolve-color-operations
groups color-c [stale-color] false)))))
(t/deftest picker-edit-falls-back-to-the-previous-color
;; While dragging the colorpicker the row color can be stale, so the
;; previously applied color is used to find the target operations.
(t/is (= [(get groups color-b) color-b]
(color-selection/resolve-color-operations
groups color-a [color-b] true))))
(t/deftest picker-edit-uses-the-row-color-on-the-first-change
(t/is (= [(get groups color-a) color-a]
(color-selection/resolve-color-operations
groups color-a [] true))))
(t/deftest color-name-and-path-do-not-affect-operation-lookup
(let [named-color (assoc color-a :name "Yellow 1" :path "palette")]
(t/is (= [(get groups color-a) color-a]
(color-selection/resolve-color-operations
groups named-color nil false)))))