0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-31 19:39:07 -05:00

🐛 Fix strokes don't update correctly

This commit is contained in:
Alejandro Alonso 2025-01-10 15:12:05 +01:00 committed by Andrey Antukh
parent 3af44641de
commit 642b6b1621
6 changed files with 95 additions and 65 deletions

View file

@ -242,59 +242,88 @@
(if (= (:type shape) :frame)
(d/merge shape attrs)
shape))))))))
(defn change-stroke
([ids attrs index] (change-stroke ids attrs index nil))
([ids attrs index options]
(ptk/reify ::change-stroke
(def ^:private stroke-style-attrs
[:stroke-style
:stroke-alignment
:stroke-width
:stroke-cap-start
:stroke-cap-end])
(defn- build-stroke-style-attrs
[stroke]
(let [attrs (select-keys stroke stroke-style-attrs)]
(cond-> attrs
(not (contains? attrs :stroke-width))
(assoc :stroke-width 1)
(not (contains? attrs :stroke-style))
(assoc :stroke-style :solid)
(not (contains? attrs :stroke-alignment))
(assoc :stroke-alignment :inner)
:always
(d/without-nils))))
(defn change-stroke-color
([ids color index] (change-stroke-color ids color index nil))
([ids color index options]
(ptk/reify ::change-stroke-color
ptk/WatchEvent
(watch [_ _ _]
(let [color-attrs (cond-> {}
(contains? attrs :color)
(assoc :stroke-color (:color attrs))
(rx/of (dwsh/update-shapes
ids
(fn [shape]
(let [stroke (get-in shape [:strokes index])
attrs (cond-> (build-stroke-style-attrs stroke)
(contains? color :color)
(assoc :stroke-color (:color color))
(contains? attrs :id)
(assoc :stroke-color-ref-id (:id attrs))
(contains? color :id)
(assoc :stroke-color-ref-id (:id color))
(contains? attrs :file-id)
(assoc :stroke-color-ref-file (:file-id attrs))
(contains? color :file-id)
(assoc :stroke-color-ref-file (:file-id color))
(contains? attrs :gradient)
(assoc :stroke-color-gradient (:gradient attrs))
(contains? color :gradient)
(assoc :stroke-color-gradient (:gradient color))
(contains? attrs :opacity)
(assoc :stroke-opacity (:opacity attrs))
(contains? color :opacity)
(assoc :stroke-opacity (:opacity color))
(contains? attrs :image)
(assoc :stroke-image (:image attrs)))
(contains? color :image)
(assoc :stroke-image (:image color))
attrs (->
(merge attrs color-attrs)
(dissoc :image)
(dissoc :gradient))]
:always
(d/without-nils))]
(cond-> shape
(not (contains? shape :strokes))
(assoc :strokes [])
(rx/of (dwsh/update-shapes
ids
(fn [shape]
(let [new-attrs (merge (get-in shape [:strokes index]) attrs)
new-attrs (cond-> new-attrs
(not (contains? new-attrs :stroke-width))
(assoc :stroke-width 1)
:always
(assoc-in [:strokes index] attrs))))
options))))))
(not (contains? new-attrs :stroke-style))
(assoc :stroke-style :solid)
(defn change-stroke-attrs
([ids attrs index] (change-stroke-attrs ids attrs index nil))
([ids attrs index options]
(ptk/reify ::change-stroke-attrs
ptk/WatchEvent
(watch [_ _ _]
(rx/of (dwsh/update-shapes
ids
(fn [shape]
(let [stroke (get-in shape [:strokes index])
style-attrs (build-stroke-style-attrs stroke)
attrs (merge stroke style-attrs attrs)]
(cond-> shape
(not (contains? shape :strokes))
(assoc :strokes [])
(not (contains? new-attrs :stroke-alignment))
(assoc :stroke-alignment :inner)
:always
(d/without-nils))]
(cond-> shape
(not (contains? shape :strokes))
(assoc :strokes [])
:always
(assoc-in [:strokes index] new-attrs))))
options)))))))
:always
(assoc-in [:strokes index] attrs)))))
options)))))
(defn change-shadow
[ids attrs index]
@ -495,7 +524,7 @@
(rx/map (fn [{:keys [shape-id index] :as operation}]
(case (:prop operation)
:fill (change-fill [shape-id] new-color index)
:stroke (change-stroke [shape-id] new-color index)
:stroke (change-stroke-color [shape-id] new-color index)
:shadow (change-shadow [shape-id] new-color index)
:content (dwt/update-text-with-function
shape-id
@ -533,7 +562,7 @@
(recur (rest pending) result))))]
(if stroke?
(rx/of (change-stroke ids color 0))
(rx/of (change-stroke-color ids color 0))
(rx/of (change-fill ids color 0)))))))
(declare activate-colorpicker-color)

View file

@ -30,8 +30,9 @@
(defn resolve
([router id] (resolve router id {}))
([router id params]
(when-let [match (r/match-by-name router id)]
(r/match->path match params))))
(when router
(when-let [match (r/match-by-name router id)]
(r/match->path match params)))))
(defn create
[routes]

View file

@ -56,7 +56,7 @@
(mf/use-fn
(mf/deps ids)
(fn [index color]
(st/emit! (dc/change-stroke ids color index))))
(st/emit! (dc/change-stroke-color ids color index))))
on-remove
@ -77,7 +77,7 @@
(fn [index color]
(let [color (-> color
(assoc :id nil :file-id nil))]
(st/emit! (dc/change-stroke ids color index)))))
(st/emit! (dc/change-stroke-color ids color index)))))
handle-reorder
(mf/use-fn
@ -90,17 +90,17 @@
(mf/use-fn
(mf/deps ids)
(fn [index value]
(st/emit! (dc/change-stroke ids {:stroke-style value} index))))
(st/emit! (dc/change-stroke-attrs ids {:stroke-style value} index))))
on-stroke-alignment-change
(fn [index value]
(when-not (str/empty? value)
(st/emit! (dc/change-stroke ids {:stroke-alignment value} index))))
(st/emit! (dc/change-stroke-attrs ids {:stroke-alignment value} index))))
on-stroke-width-change
(fn [index value]
(when-not (str/empty? value)
(st/emit! (dc/change-stroke ids {:stroke-width value} index))))
(st/emit! (dc/change-stroke-attrs ids {:stroke-width value} index))))
open-caps-select
(fn [caps-state]
@ -128,11 +128,11 @@
on-stroke-cap-start-change
(fn [index value]
(st/emit! (dc/change-stroke ids {:stroke-cap-start value} index)))
(st/emit! (dc/change-stroke-attrs ids {:stroke-cap-start value} index)))
on-stroke-cap-end-change
(fn [index value]
(st/emit! (dc/change-stroke ids {:stroke-cap-end value} index)))
(st/emit! (dc/change-stroke-attrs ids {:stroke-cap-end value} index)))
on-stroke-cap-switch
(fn [index]
@ -140,8 +140,8 @@
stroke-cap-end (get-in values [:strokes index :stroke-cap-end])]
(when (and (not= stroke-cap-start :multiple)
(not= stroke-cap-end :multiple))
(st/emit! (dc/change-stroke ids {:stroke-cap-start stroke-cap-end
:stroke-cap-end stroke-cap-start} index)))))
(st/emit! (dc/change-stroke-attrs ids {:stroke-cap-start stroke-cap-end
:stroke-cap-end stroke-cap-start} index)))))
on-add-stroke
(fn [_]
(st/emit! (dc/add-stroke ids {:stroke-alignment :inner

View file

@ -147,7 +147,7 @@
(defn update-stroke-color
[value shape-ids]
(update-color wdc/change-stroke value shape-ids))
(update-color wdc/change-stroke-color value shape-ids))
(defn update-fill-stroke [value shape-ids attributes]
(ptk/reify ::update-fill-stroke

View file

@ -48,7 +48,7 @@
(t/is (= (:fill-color fill') "#fabada"))
(t/is (= (:fill-opacity fill') 1))))))))
(t/deftest test-update-stroke
(t/deftest test-update-stroke-color
;; Old shapes without stroke-alignment are rendered as if it is centered
(t/async
done
@ -62,7 +62,7 @@
;; ==== Action
events
[(dc/change-stroke #{(cthi/id :shape1)} {:color "#FABADA"} 0)]]
[(dc/change-stroke-color #{(cthi/id :shape1)} {:color "#FABADA"} 0)]]
(ths/run-store
store done events
@ -77,6 +77,8 @@
first)]
;; ==== Check
(println stroke')
;; (println stroke')
(t/is (some? shape1'))
(t/is (= (:stroke-alignment stroke') :inner))))))))
(t/is (= (:stroke-alignment stroke') :inner))
(t/is (= (:stroke-color stroke') "#FABADA"))
(t/is (= (:stroke-width stroke') 2))))))))

View file

@ -23,11 +23,9 @@
_ (set! st/state store)
context (api/create-context "TEST")
page (. context -currentPage)
shape (.createRectangle context)
^js context (api/create-context "TEST")
^js page (. context -currentPage)
^js shape (.createRectangle context)
get-shape-path
#(vector :workspace-data :pages-index (aget page "$id") :objects (aget shape "$id") %)]