🐛 Fix stroke token errors (#5661)

* 🐛 Fix change on any stroke attribute remove tokens

* 🐛 Fix add new stroke, reorder stroke or remove stroke should remove applied tokens

* 📎 Review fixes
This commit is contained in:
Eva Marco 2025-01-24 11:24:26 +01:00 committed by GitHub
parent 25b89ec59f
commit 231d875f79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 71 additions and 55 deletions

View file

@ -20,13 +20,13 @@
(defn- generate-unapply-tokens
"When updating attributes that have a token applied, we must unapply it, because the value
of the attribute now has been given directly, and does not come from the token."
[changes objects]
[changes objects changed-sub-attr]
(let [mod-obj-changes (->> (:redo-changes changes)
(filter #(= (:type %) :mod-obj)))
check-attr (fn [shape changes attr]
(let [tokens (get shape :applied-tokens {})
token-attrs (cto/shape-attr->token-attrs attr)]
token-attrs (cto/shape-attr->token-attrs attr changed-sub-attr)]
(if (some #(contains? tokens %) token-attrs)
(pcb/update-shapes changes [(:id shape)] #(cto/unapply-token-id % token-attrs))
changes)))
@ -44,7 +44,7 @@
mod-obj-changes)))
(defn generate-update-shapes
[changes ids update-fn objects {:keys [attrs ignore-tree ignore-touched with-objects?]}]
[changes ids update-fn objects {:keys [attrs changed-sub-attr ignore-tree ignore-touched with-objects?]}]
(let [changes (reduce
(fn [changes id]
(let [opts {:attrs attrs
@ -61,7 +61,7 @@
(pcb/reorder-grid-children ids)
(cond->
(not ignore-touched)
(generate-unapply-tokens objects)))]
(generate-unapply-tokens objects changed-sub-attr)))]
changes))
(defn- generate-update-shape-flags

View file

@ -175,15 +175,20 @@
::dimensions])
(defn shape-attr->token-attrs
[shape-attr]
([shape-attr] (shape-attr->token-attrs shape-attr nil))
([shape-attr changed-sub-attr]
(cond
(= :fills shape-attr) #{:fill}
(= :strokes shape-attr) #{:stroke-color :stroke-width}
(and (= :strokes shape-attr) (nil? changed-sub-attr)) #{:stroke-width :stroke-color}
(= :strokes shape-attr)
(cond
(some #{:stroke-color} changed-sub-attr) #{:stroke-color}
(some #{:stroke-width} changed-sub-attr) #{:stroke-width})
(border-radius-keys shape-attr) #{shape-attr}
(sizing-keys shape-attr) #{shape-attr}
(opacity-keys shape-attr) #{shape-attr}
(spacing-keys shape-attr) #{shape-attr}
(rotation-keys shape-attr) #{shape-attr}))
(rotation-keys shape-attr) #{shape-attr})))
(defn token-attr->shape-attr
[token-attr]

View file

@ -272,7 +272,8 @@
(ptk/reify ::change-stroke-color
ptk/WatchEvent
(watch [_ _ _]
(rx/of (dwsh/update-shapes
(rx/of (let [options (assoc options :changed-sub-attr [:stroke-color])]
(dwsh/update-shapes
ids
(fn [shape]
(let [stroke (get-in shape [:strokes index])
@ -303,7 +304,7 @@
:always
(assoc-in [:strokes index] attrs))))
options))))))
options)))))))
(defn change-stroke-attrs
([ids attrs index] (change-stroke-attrs ids attrs index nil))
@ -311,6 +312,8 @@
(ptk/reify ::change-stroke-attrs
ptk/WatchEvent
(watch [_ _ _]
(let [changed-sub-attr (keys attrs)
options (assoc options :changed-sub-attr changed-sub-attr)]
(rx/of (dwsh/update-shapes
ids
(fn [shape]
@ -322,8 +325,8 @@
(assoc :strokes [])
:always
(assoc-in [:strokes index] attrs)))))
options)))))
(assoc-in [:strokes index] attrs))))
options)))))))
(defn change-shadow
[ids attrs index]
@ -379,7 +382,9 @@
ptk/WatchEvent
(watch [_ _ _]
(let [add-stroke (fn [shape] (update shape :strokes #(into [stroke] %)))]
(rx/of (dwsh/update-shapes ids add-stroke))))))
(rx/of (dwsh/update-shapes ids
add-stroke
{:attrs [:strokes]}))))))
(defn remove-stroke
[ids position]
@ -397,7 +402,9 @@
(mapv second)))
(remove-stroke [shape]
(update shape :strokes remove-fill-by-index position))]
(rx/of (dwsh/update-shapes ids remove-stroke))))))
(rx/of (dwsh/update-shapes ids
remove-stroke
{:attrs [:strokes]}))))))
(defn remove-all-strokes
[ids]
@ -410,7 +417,9 @@
ptk/WatchEvent
(watch [_ _ _]
(let [remove-all #(assoc % :strokes [])]
(rx/of (dwsh/update-shapes ids remove-all))))))
(rx/of (dwsh/update-shapes ids
remove-all
{:attrs [:strokes]}))))))
(defn reorder-shadows
[ids index new-index]
@ -428,7 +437,8 @@
(watch [_ _ _]
(rx/of (dwsh/update-shapes
ids
#(swap-attrs % :strokes index new-index))))))
#(swap-attrs % :strokes index new-index)
{:attrs [:strokes]})))))
(defn picker-for-selected-shape
[]

View file

@ -47,7 +47,7 @@
(defn update-shapes
([ids update-fn] (update-shapes ids update-fn nil))
([ids update-fn {:keys [reg-objects? save-undo? stack-undo? attrs ignore-tree page-id ignore-touched undo-group with-objects?]
([ids update-fn {:keys [reg-objects? save-undo? stack-undo? attrs ignore-tree page-id ignore-touched undo-group with-objects? changed-sub-attr]
:or {reg-objects? false save-undo? true stack-undo? false ignore-touched false with-objects? false}}]
(dm/assert!
@ -76,6 +76,7 @@
update-fn
objects
{:attrs attrs
:changed-sub-attr changed-sub-attr
:ignore-tree ignore-tree
:ignore-touched ignore-touched
:with-objects? with-objects?})