🐛 Leave gradient mode when undo removes a stroke gradient (#13262)

* 🐛 Leave gradient mode when undo removes a stroke gradient

Undoing a gradient with the color picker open sent a plain color.
The picker dropped the stops but stayed in gradient mode, so adding
a stop built a gradient with no coordinates or colors and applied it
to the stroke. The backend rejected it as an invalid shape.

Switch the picker to color (or image) when the color is no longer a
gradient, and skip invalid gradients before applying them.

Closes #13261

AI-assisted-by: claude-opus-5-5

* 🐛 Cover more colorpicker cases in the stroke gradient tests

From the review: plain and image colors still reach on-change through
the gradient guard, both gradient types leave the gradient tab, and a
plain color keeps the color tab. The image tab test is renamed after
what it checks.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Eva Marco 2026-11-24 16:07:17 +01:00 committed by GitHub
parent 915d405a9c
commit 590c3add36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 2 deletions

View File

@ -872,7 +872,11 @@
ptk/WatchEvent
(watch [_ state _]
(when-let [color (some-> state :colorpicker get-color-from-colorpicker-state)]
(on-change color)))))
;; A gradient without stops or coordinates is not a valid color;
;; applying it would send an invalid shape to the backend.
(when (or (nil? (:gradient color))
(clr/valid-color? color))
(on-change color))))))
(defn initialize-colorpicker
[on-change tab]
@ -942,7 +946,11 @@
(assoc :current-color (split-color-components (dissoc data :gradient))))
(dissoc :editing-stop)
(dissoc :gradient)
(dissoc :stops))))))))))
(dissoc :stops)
;; The color is no longer a gradient (e.g. after an
;; undo), so leave the gradient tab too.
(cond-> (contains? #{:linear-gradient :radial-gradient} (:type state))
(assoc :type (if (:image data) :image :color))))))))))))
(defn update-colorpicker-gradient-opacity
[opacity]

View File

@ -105,3 +105,68 @@
:current-color {:color "#ff0000" :opacity 1}}}
event (dwc/update-colorpicker-color {} true)]
(t/is (some? (ptk/watch event valid-state nil)))))
(def ^:private gradient-colorpicker
{:type :linear-gradient
:editing-stop 0
:gradient {:type :linear :start-x 0.5 :start-y 0 :end-x 0.5 :end-y 1 :width 1}
:stops [{:color "#000000" :opacity 1 :offset 0}
{:color "#000000" :opacity 0 :offset 1}]
:current-color {:color "#000000" :opacity 1 :offset 0}})
(t/deftest update-colorpicker-leaves-gradient-tab-on-plain-color
;; Regression for https://github.com/penpot/penpot/issues/12463.
;; Undoing a gradient with the colorpicker open sends a plain color.
;; The colorpicker dropped the stops but stayed in gradient mode, so
;; adding a stop built a gradient without coordinates or colors.
(doseq [type [:linear-gradient :radial-gradient]]
(t/testing (name type)
(let [state {:colorpicker (assoc gradient-colorpicker :type type)}
event (dwc/update-colorpicker {:color "#000000" :opacity 1})
result (:colorpicker (ptk/update event state))]
(t/is (= :color (:type result)))
(t/is (nil? (:stops result)))
(t/is (nil? (:gradient result)))))))
(t/deftest update-colorpicker-switches-to-image-tab-on-image-color
(let [state {:colorpicker gradient-colorpicker}
image {:id uuid/zero :width 10 :height 10 :mtype "image/png"}
event (dwc/update-colorpicker {:image image :opacity 1})
result (:colorpicker (ptk/update event state))]
(t/is (= :image (:type result)))))
(t/deftest update-colorpicker-keeps-color-tab-on-plain-color
(let [state {:colorpicker {:type :color
:current-color {:color "#000000" :opacity 1}}}
event (dwc/update-colorpicker {:color "#ff0000" :opacity 1})
result (:colorpicker (ptk/update event state))]
(t/is (= :color (:type result)))
(t/is (= "#ff0000" (get-in result [:current-color :color])))))
(t/deftest colorpicker-onchange-runner-skips-invalid-gradient
(let [calls (atom [])
runner (#'dwc/colorpicker-onchange-runner #(swap! calls conj %))
empty-gradient {:colorpicker {:type :linear-gradient
:stops [{:offset 0.5}]}}]
(ptk/watch runner empty-gradient nil)
(t/is (empty? @calls))
(ptk/watch runner {:colorpicker gradient-colorpicker} nil)
(t/is (= 1 (count @calls)))
(t/is (= [{:color "#000000" :opacity 1 :offset 0}
{:color "#000000" :opacity 0 :offset 1}]
(get-in (first @calls) [:gradient :stops])))))
(t/deftest colorpicker-onchange-runner-applies-plain-and-image-colors
;; The guard only drops invalid gradients: colors without a gradient
;; must still reach `on-change`.
(let [calls (atom [])
runner (#'dwc/colorpicker-onchange-runner #(swap! calls conj %))
image {:id uuid/zero :width 10 :height 10 :mtype "image/png"}]
(ptk/watch runner {:colorpicker {:type :color
:current-color {:color "#ff0000" :opacity 1}}} nil)
(ptk/watch runner {:colorpicker {:type :image
:current-color {:image image :opacity 1}}} nil)
(t/is (= 2 (count @calls)))
(t/is (= "#ff0000" (:color (first @calls))))
(t/is (= image (:image (second @calls))))))