From c0306b118dc010f6f5a37fcc404795761de8f9bf Mon Sep 17 00:00:00 2001 From: Eva Marco Date: Wed, 29 Jan 2025 17:37:09 +0100 Subject: [PATCH] :bug: Fix thumbnail regeration when changing sets of tokens --- frontend/src/app/main/data/helpers.cljs | 8 +- .../src/app/main/data/workspace/colors.cljs | 3 +- .../src/app/main/data/workspace/layout.cljs | 2 - .../app/main/data/workspace/libraries.cljs | 4 +- .../app/main/data/workspace/modifiers.cljs | 11 ++- .../app/main/data/workspace/shape_layout.cljs | 3 +- .../app/main/data/workspace/thumbnails.cljs | 10 +-- .../app/main/data/workspace/transforms.cljs | 35 ++++---- .../app/main/ui/workspace/tokens/changes.cljs | 14 +-- .../app/main/ui/workspace/tokens/update.cljs | 89 ++++++++++++------- 10 files changed, 103 insertions(+), 76 deletions(-) diff --git a/frontend/src/app/main/data/helpers.cljs b/frontend/src/app/main/data/helpers.cljs index e6151f4973..b104af0dcd 100644 --- a/frontend/src/app/main/data/helpers.cljs +++ b/frontend/src/app/main/data/helpers.cljs @@ -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 diff --git a/frontend/src/app/main/data/workspace/colors.cljs b/frontend/src/app/main/data/workspace/colors.cljs index fbc23b0bdc..0fd1239f1b 100644 --- a/frontend/src/app/main/data/workspace/colors.cljs +++ b/frontend/src/app/main/data/workspace/colors.cljs @@ -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) diff --git a/frontend/src/app/main/data/workspace/layout.cljs b/frontend/src/app/main/data/workspace/layout.cljs index c99deb8714..89e8a90d8a 100644 --- a/frontend/src/app/main/data/workspace/layout.cljs +++ b/frontend/src/app/main/data/workspace/layout.cljs @@ -187,5 +187,3 @@ (if (= val ::does-not-exist) (swap! storage/user dissoc skey) (swap! storage/user assoc skey val))))) - - diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index fa8157e6cb..95d1df358e 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -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 diff --git a/frontend/src/app/main/data/workspace/modifiers.cljs b/frontend/src/app/main/data/workspace/modifiers.cljs index 63d96e867b..21fb5487f9 100644 --- a/frontend/src/app/main/data/workspace/modifiers.cljs +++ b/frontend/src/app/main/data/workspace/modifiers.cljs @@ -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 diff --git a/frontend/src/app/main/data/workspace/shape_layout.cljs b/frontend/src/app/main/data/workspace/shape_layout.cljs index af3bc4afcf..9852c74688 100644 --- a/frontend/src/app/main/data/workspace/shape_layout.cljs +++ b/frontend/src/app/main/data/workspace/shape_layout.cljs @@ -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 %))) diff --git a/frontend/src/app/main/data/workspace/thumbnails.cljs b/frontend/src/app/main/data/workspace/thumbnails.cljs index 28d81a781a..847604cbb0 100644 --- a/frontend/src/app/main/data/workspace/thumbnails.cljs +++ b/frontend/src/app/main/data/workspace/thumbnails.cljs @@ -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`" diff --git a/frontend/src/app/main/data/workspace/transforms.cljs b/frontend/src/app/main/data/workspace/transforms.cljs index e712224fce..45e57c6cd2 100644 --- a/frontend/src/app/main/data/workspace/transforms.cljs +++ b/frontend/src/app/main/data/workspace/transforms.cljs @@ -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 diff --git a/frontend/src/app/main/ui/workspace/tokens/changes.cljs b/frontend/src/app/main/ui/workspace/tokens/changes.cljs index 40111b760d..2cc4f90ef8 100644 --- a/frontend/src/app/main/ui/workspace/tokens/changes.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/changes.cljs @@ -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)) diff --git a/frontend/src/app/main/ui/workspace/tokens/update.cljs b/frontend/src/app/main/ui/workspace/tokens/update.cljs index 81934b062e..c5f6d5ae0c 100644 --- a/frontend/src/app/main/ui/workspace/tokens/update.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/update.cljs @@ -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)))))))))))