🐛 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 (defn- generate-unapply-tokens
"When updating attributes that have a token applied, we must unapply it, because the value "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." 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) (let [mod-obj-changes (->> (:redo-changes changes)
(filter #(= (:type %) :mod-obj))) (filter #(= (:type %) :mod-obj)))
check-attr (fn [shape changes attr] check-attr (fn [shape changes attr]
(let [tokens (get shape :applied-tokens {}) (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) (if (some #(contains? tokens %) token-attrs)
(pcb/update-shapes changes [(:id shape)] #(cto/unapply-token-id % token-attrs)) (pcb/update-shapes changes [(:id shape)] #(cto/unapply-token-id % token-attrs))
changes))) changes)))
@ -44,7 +44,7 @@
mod-obj-changes))) mod-obj-changes)))
(defn generate-update-shapes (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 (let [changes (reduce
(fn [changes id] (fn [changes id]
(let [opts {:attrs attrs (let [opts {:attrs attrs
@ -61,7 +61,7 @@
(pcb/reorder-grid-children ids) (pcb/reorder-grid-children ids)
(cond-> (cond->
(not ignore-touched) (not ignore-touched)
(generate-unapply-tokens objects)))] (generate-unapply-tokens objects changed-sub-attr)))]
changes)) changes))
(defn- generate-update-shape-flags (defn- generate-update-shape-flags

View file

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

View file

@ -272,38 +272,39 @@
(ptk/reify ::change-stroke-color (ptk/reify ::change-stroke-color
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ _] (watch [_ _ _]
(rx/of (dwsh/update-shapes (rx/of (let [options (assoc options :changed-sub-attr [:stroke-color])]
ids (dwsh/update-shapes
(fn [shape] ids
(let [stroke (get-in shape [:strokes index]) (fn [shape]
attrs (cond-> (build-stroke-style-attrs stroke) (let [stroke (get-in shape [:strokes index])
(contains? color :color) attrs (cond-> (build-stroke-style-attrs stroke)
(assoc :stroke-color (:color color)) (contains? color :color)
(assoc :stroke-color (:color color))
(contains? color :id) (contains? color :id)
(assoc :stroke-color-ref-id (:id color)) (assoc :stroke-color-ref-id (:id color))
(contains? color :file-id) (contains? color :file-id)
(assoc :stroke-color-ref-file (:file-id color)) (assoc :stroke-color-ref-file (:file-id color))
(contains? color :gradient) (contains? color :gradient)
(assoc :stroke-color-gradient (:gradient color)) (assoc :stroke-color-gradient (:gradient color))
(contains? color :opacity) (contains? color :opacity)
(assoc :stroke-opacity (:opacity color)) (assoc :stroke-opacity (:opacity color))
(contains? color :image) (contains? color :image)
(assoc :stroke-image (:image color)) (assoc :stroke-image (:image color))
:always :always
(d/without-nils))] (d/without-nils))]
(cond-> shape (cond-> shape
(not (contains? shape :strokes)) (not (contains? shape :strokes))
(assoc :strokes []) (assoc :strokes [])
:always :always
(assoc-in [:strokes index] attrs)))) (assoc-in [:strokes index] attrs))))
options)))))) options)))))))
(defn change-stroke-attrs (defn change-stroke-attrs
([ids attrs index] (change-stroke-attrs ids attrs index nil)) ([ids attrs index] (change-stroke-attrs ids attrs index nil))
@ -311,19 +312,21 @@
(ptk/reify ::change-stroke-attrs (ptk/reify ::change-stroke-attrs
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ _] (watch [_ _ _]
(rx/of (dwsh/update-shapes (let [changed-sub-attr (keys attrs)
ids options (assoc options :changed-sub-attr changed-sub-attr)]
(fn [shape] (rx/of (dwsh/update-shapes
(let [stroke (get-in shape [:strokes index]) ids
style-attrs (build-stroke-style-attrs stroke) (fn [shape]
attrs (merge stroke style-attrs attrs)] (let [stroke (get-in shape [:strokes index])
(cond-> shape style-attrs (build-stroke-style-attrs stroke)
(not (contains? shape :strokes)) attrs (merge stroke style-attrs attrs)]
(assoc :strokes []) (cond-> shape
(not (contains? shape :strokes))
(assoc :strokes [])
:always :always
(assoc-in [:strokes index] attrs))))) (assoc-in [:strokes index] attrs))))
options))))) options)))))))
(defn change-shadow (defn change-shadow
[ids attrs index] [ids attrs index]
@ -379,7 +382,9 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ _] (watch [_ _ _]
(let [add-stroke (fn [shape] (update shape :strokes #(into [stroke] %)))] (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 (defn remove-stroke
[ids position] [ids position]
@ -397,7 +402,9 @@
(mapv second))) (mapv second)))
(remove-stroke [shape] (remove-stroke [shape]
(update shape :strokes remove-fill-by-index position))] (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 (defn remove-all-strokes
[ids] [ids]
@ -410,7 +417,9 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ _] (watch [_ _ _]
(let [remove-all #(assoc % :strokes [])] (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 (defn reorder-shadows
[ids index new-index] [ids index new-index]
@ -428,7 +437,8 @@
(watch [_ _ _] (watch [_ _ _]
(rx/of (dwsh/update-shapes (rx/of (dwsh/update-shapes
ids ids
#(swap-attrs % :strokes index new-index)))))) #(swap-attrs % :strokes index new-index)
{:attrs [:strokes]})))))
(defn picker-for-selected-shape (defn picker-for-selected-shape
[] []

View file

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