🐛 Fix thumbnail regeration when changing sets of tokens

This commit is contained in:
Eva Marco 2025-01-29 17:37:09 +01:00
parent 6cdef04968
commit c0306b118d
10 changed files with 103 additions and 76 deletions

View File

@ -36,12 +36,6 @@
([state file-id]
(dm/get-in state [:files file-id :data])))
(defn get-all-page-ids
([state]
(get-all-page-ids state (:current-file-id state)))
([state file-id]
(dm/get-in state [:files file-id :data :pages])))
(defn get-page
[fdata page-id]
(dm/get-in fdata [:pages-index page-id]))
@ -67,7 +61,7 @@
(:current-file-id state)
page-id))
([state file-id page-id]
(-> (lookup-page state file-id (d/nilv page-id (:current-page-id state)))
(-> (lookup-page state file-id page-id)
(get :objects))))
(defn process-selected-shapes

View File

@ -84,7 +84,8 @@
(defn transform-fill
([state ids color transform] (transform-fill state ids color transform nil))
([state ids color transform options]
(let [page-id (get options :page-id (get state :current-page-id))
(let [page-id (or (get options :page-id)
(get state :current-page-id))
objects (dsh/lookup-page-objects state page-id)
is-text? #(= :text (:type (get objects %)))
text-ids (filter is-text? ids)

View File

@ -187,5 +187,3 @@
(if (= val ::does-not-exist)
(swap! storage/user dissoc skey)
(swap! storage/user assoc skey val)))))

View File

@ -761,7 +761,7 @@
(ptk/reify ::library-thumbnails-fetched
ptk/UpdateEvent
(update [_ state]
(update state :workspace-thumbnails merge thumbnails))))
(update state :thumbnails merge thumbnails))))
(defn fetch-library-thumbnails
[library-id]
@ -1408,7 +1408,7 @@
(->> (rp/cmd! :get-file-object-thumbnails {:file-id library-id :tag "component"})
(rx/map (fn [thumbnails]
(fn [state]
(update state :workspace-thumbnails merge thumbnails))))))
(update state :thumbnails merge thumbnails))))))
(rx/of (ptk/reify ::attach-library-finished)))))))
(defn unlink-file-from-library

View File

@ -334,7 +334,8 @@
(calculate-modifiers state ignore-constraints ignore-snap-pixel modif-tree page-id nil))
([state ignore-constraints ignore-snap-pixel modif-tree page-id params]
(let [page-id (d/nilv page-id (:current-page-id state))
(let [page-id
(or page-id (:current-page-id state))
objects
(dsh/lookup-page-objects state page-id)
@ -404,7 +405,9 @@
(ptk/reify ::set-modifiers
ptk/UpdateEvent
(update [_ state]
(assoc state :workspace-modifiers (calculate-modifiers state ignore-constraints ignore-snap-pixel modif-tree params))))))
(let [page-id (:current-page-id state)
modifiers (calculate-modifiers state ignore-constraints ignore-snap-pixel modif-tree page-id params)]
(assoc state :workspace-modifiers modifiers))))))
(def ^:private
xf-rotation-shape
@ -444,7 +447,7 @@
(ptk/reify ::set-delta-rotation-modifiers
ptk/UpdateEvent
(update [_ state]
(let [page-id (d/nilv page-id (:current-page-id state))
(let [page-id (or page-id (:current-page-id state))
objects (dsh/lookup-page-objects state page-id)
ids
(->> shapes
@ -476,7 +479,7 @@
ptk/WatchEvent
(watch [_ state _]
(let [text-modifiers (get state :workspace-text-modifier)
page-id (d/nilv page-id (:current-page-id state))
page-id (or page-id (:current-page-id state))
objects (dsh/lookup-page-objects state page-id)
object-modifiers

View File

@ -524,7 +524,8 @@
(ptk/reify ::update-layout-child
ptk/WatchEvent
(watch [_ state _]
(let [page-id (get options :page-id)
(let [page-id (or (get options :page-id)
(get state :current-page-id))
objects (dsh/lookup-page-objects state page-id)
children-ids (->> ids (mapcat #(cfh/get-children-ids objects %)))
parent-ids (->> ids (map #(cfh/get-parent-id objects %)))

View File

@ -94,7 +94,7 @@
ptk/UpdateEvent
(update [_ state]
(update state :workspace-thumbnails
(update state :thumbnails
(fn [thumbs]
(if-let [uri (get thumbs object-id)]
(do (vreset! pending uri)
@ -122,10 +122,10 @@
(ptk/reify ::assoc-thumbnail
ptk/UpdateEvent
(update [_ state]
(let [prev-uri (dm/get-in state [:workspace-thumbnails object-id])]
(let [prev-uri (dm/get-in state [:thumbnails object-id])]
(some->> prev-uri (vreset! prev-uri*))
(l/trc :hint "assoc thumbnail" :object-id object-id :uri uri)
(update state :workspace-thumbnails assoc object-id uri)))
(update state :thumbnails assoc object-id uri)))
ptk/EffectEvent
(effect [_ _ _]
@ -141,8 +141,8 @@
(update [_ state]
(let [old-id (dm/str old-id)
new-id (dm/str new-id)
thumbnail (dm/get-in state [:workspace-thumbnails old-id])]
(update state :workspace-thumbnails assoc new-id thumbnail)))))
thumbnail (dm/get-in state [:thumbnails old-id])]
(update state :thumbnails assoc new-id thumbnail)))))
(defn update-thumbnail
"Updates the thumbnail information for the given `id`"

View File

@ -314,13 +314,14 @@
(ptk/reify ::update-dimensions
ptk/UpdateEvent
(update [_ state]
(let [page-id (get options :page-id)
(let [page-id (or (get options :page-id)
(get state :current-page-id))
objects (dsh/lookup-page-objects state page-id)
get-modifier
(fn [shape] (ctm/change-dimensions-modifiers shape attr value))
modif-tree
(-> (dwm/build-modif-tree ids objects get-modifier)
(gm/set-objects-modifiers objects))]
get-modifier (fn [shape]
(ctm/change-dimensions-modifiers shape attr value))
modif-tree (-> (dwm/build-modif-tree ids objects get-modifier)
(gm/set-objects-modifiers objects))]
(assoc state :workspace-modifiers modif-tree)))
@ -408,14 +409,14 @@
(ptk/reify ::increase-rotation
ptk/WatchEvent
(watch [_ state _]
(let [page-id (d/nilv (:page-id params) (:current-page-id state))
(let [page-id (or (:page-id options)
(:current-page-id state))
objects (dsh/lookup-page-objects state page-id)
shapes (->> ids (map #(get objects %)))]
shapes (->> ids (map #(get objects %)))
options (assoc options :page-id page-id)]
(rx/concat
(rx/of (dwm/set-delta-rotation-modifiers rotation shapes params))
(rx/of (dwm/apply-modifiers (assoc options :page-id page-id)))))))))
(rx/of (dwm/apply-modifiers options))))))))
;; -- Move ----------------------------------------------------------
(declare start-move)
@ -788,14 +789,15 @@
(defn update-position
"Move shapes to a new position"
([id position] (update-position nil id position nil))
([page-id id position opts]
([id position] (update-position id position nil))
([id position options]
(dm/assert! (uuid? id))
(ptk/reify ::update-position
ptk/WatchEvent
(watch [_ state _]
(let [page-id (d/nilv page-id (:current-page-id state))
(let [page-id (or (get options :page-id)
(get state :current-page-id))
objects (dsh/lookup-page-objects state page-id)
shape (get objects id)
;; FIXME: performance rect
@ -809,10 +811,11 @@
modif-tree (dwm/create-modif-tree [id] (ctm/move-modifiers delta))]
;; FIXME: should we pass page-id down to apply-modifiers (?)
(rx/of (dwm/apply-modifiers {:modifiers modif-tree
:page-id page-id
:ignore-constraints false
:ignore-touched (:ignore-touched opts)
:ignore-touched (:ignore-touched options)
:ignore-snap-pixel true})))))))
(defn position-shapes

View File

@ -135,6 +135,10 @@
{:ignore-touched true
:page-id page-id}))))
;; FIXME: if attributes are not always present, maybe we have an
;; options here for pass optional value and preserve the correct and
;; uniform api across all functions (?)
(defn update-rotation
([value shape-ids attributes] (update-rotation value shape-ids attributes nil))
([value shape-ids _attributes page-id] ; The attributes param is needed to have the same arity that other update functions
@ -143,9 +147,9 @@
(watch [_ _ _]
(rx/of
(udw/trigger-bounding-box-cloaking shape-ids)
(udw/increase-rotation shape-ids value
{:page-id page-id}
:ignore-touched true))))))
(udw/increase-rotation shape-ids value nil
{:page-id page-id
:ignore-touched true}))))))
(defn update-stroke-width
([value shape-ids attributes] (update-stroke-width value shape-ids attributes nil))
@ -213,7 +217,7 @@
(ptk/reify ::update-layout-padding
ptk/WatchEvent
(watch [_ state _]
(let [ids-with-layout (shape-ids-with-layout state page-id shape-ids)]
(let [ids-with-layout (shape-ids-with-layout state (or page-id (:current-page-id state)) shape-ids)]
(rx/of
(dwsl/update-layout ids-with-layout
{:layout-padding (zipmap attrs (repeat value))}
@ -241,7 +245,7 @@
ptk/WatchEvent
(watch [_ _ _]
(rx/concat
(map #(dwt/update-position page-id % (zipmap attributes (repeat value)) {:ignore-touched true}) shape-ids))))))
(map #(dwt/update-position % (zipmap attributes (repeat value)) {:ignore-touched true :page-id page-id}) shape-ids))))))
(defn update-layout-sizing-limits
([value shape-ids attributes] (update-layout-sizing-limits value shape-ids attributes nil))

View File

@ -1,8 +1,10 @@
(ns app.main.ui.workspace.tokens.update
(:require
[app.common.files.helpers :as cfh]
[app.common.types.token :as ctt]
[app.main.data.helpers :as dsh]
[app.main.data.workspace.shape-layout :as dwsl]
[app.main.data.workspace.thumbnails :as dwt]
[app.main.data.workspace.undo :as dwu]
[app.main.ui.workspace.tokens.changes :as wtch]
[app.main.ui.workspace.tokens.style-dictionary :as wtsd]
@ -86,18 +88,29 @@
(->> (map (fn [[value attrs]] [attrs {value #{object-id}}]) attrs-values-map)
(into {})))
(defn collect-shapes-update-info [resolved-tokens objects]
(reduce
(fn [acc [shape-id {:keys [applied-tokens] :as shape}]]
(if (seq applied-tokens)
(let [applied-tokens (-> (invert-collect-key-vals applied-tokens resolved-tokens shape)
(shape-ids-by-values shape-id)
(split-attribute-groups))]
(deep-merge acc applied-tokens))
acc))
{} objects))
(defn- collect-shapes-update-info [resolved-tokens objects]
(loop [items (seq objects)
frame-ids #{}
tokens {}]
(if-let [[shape-id {:keys [applied-tokens] :as shape}] (first items)]
(let [applied-tokens
(-> (invert-collect-key-vals applied-tokens resolved-tokens shape)
(shape-ids-by-values shape-id)
(split-attribute-groups))
(defn actionize-shapes-update-info [page-id shapes-update-info]
parent-frame-id
(cfh/get-shape-id-root-frame objects shape-id)]
(recur (rest items)
(if parent-frame-id
(conj frame-ids parent-frame-id)
frame-ids)
(deep-merge tokens applied-tokens)))
[tokens frame-ids])))
;; FIXME: revisit this
(defn- actionize-shapes-update-info [page-id shapes-update-info]
(mapcat (fn [[attrs update-infos]]
(let [action (some attribute-actions-map attrs)]
(map
@ -106,30 +119,40 @@
update-infos)))
shapes-update-info))
(defn update-tokens-in-page [state page-id resolved-tokens]
(->> (dsh/lookup-page-objects state page-id)
(collect-shapes-update-info resolved-tokens)
(actionize-shapes-update-info page-id)))
(defn update-tokens
[state resolved-tokens]
(let [file-id (get state :current-file-id)
fdata (dsh/lookup-file-data state file-id)]
(->> (rx/from (:pages fdata))
(rx/mapcat
(fn [page-id]
(let [page
(dsh/get-page fdata page-id)
(defn update-tokens [state resolved-tokens]
(reduce (fn [events page-id]
(rx/concat events (update-tokens-in-page state page-id resolved-tokens)))
(rx/empty)
(dsh/get-all-page-ids state)))
[attrs frame-ids]
(collect-shapes-update-info resolved-tokens (:objects page))
(defn update-workspace-tokens []
actions
(actionize-shapes-update-info page-id attrs)]
(rx/merge
(rx/from actions)
(->> (rx/from frame-ids)
(rx/mapcat (fn [frame-id]
(rx/of (dwt/clear-thumbnail file-id page-id frame-id "frame")
(dwt/clear-thumbnail file-id page-id frame-id "component"))))))))))))
(defn update-workspace-tokens
[]
(ptk/reify ::update-workspace-tokens
ptk/WatchEvent
(watch [_ state _]
(->>
(rx/from
(->
(wtts/get-active-theme-sets-tokens-names-map state)
(wtsd/resolve-tokens+)))
(rx/mapcat
(fn [sd-tokens]
(let [undo-id (js/Symbol)]
(rx/concat
(rx/of (dwu/start-undo-transaction undo-id))
(update-tokens state sd-tokens)
(rx/of (dwu/commit-undo-transaction undo-id))))))))))
(let [tokens (-> (wtts/get-active-theme-sets-tokens-names-map state)
(wtsd/resolve-tokens+))]
(->> (rx/from tokens)
(rx/mapcat (fn [sd-tokens]
(let [undo-id (js/Symbol)]
(rx/concat
(rx/of (dwu/start-undo-transaction undo-id))
(update-tokens state sd-tokens)
(rx/of (dwu/commit-undo-transaction undo-id)))))))))))