From c0c8390a7d7a0b9f670526908df3c91bdf43b259 Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Tue, 19 Aug 2025 10:24:43 +0200 Subject: [PATCH] :bug: Fix restoring component inside flex --- frontend/src/app/main/data/workspace.cljs | 2 +- .../app/main/data/workspace/libraries.cljs | 36 +++++++++++++------ .../ui/workspace/sidebar/assets/common.cljs | 26 +++++++------- .../workspace/sidebar/assets/components.cljs | 4 +-- .../sidebar/options/menus/component.cljs | 5 +-- 5 files changed, 45 insertions(+), 28 deletions(-) diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index f4b3216b53..cc418bd536 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -375,7 +375,7 @@ (rx/filter (ptk/type? ::workspace-initialized)) (rx/observe-on :async) (rx/take 1) - (rx/map #(dwl/go-to-local-component :id component-id)))) + (rx/map #(dwl/go-to-local-component :id component-id :update-layout? (:update-layout rparams))))) (when (:board-id rparams) (->> stream diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 43ce608d29..aeb9f3f3cc 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -527,14 +527,17 @@ (ptk/reify ::restore-component ptk/WatchEvent (watch [it state _] - (let [page-id (:current-page-id state) - page (dsh/lookup-page state page-id) - objects (:objects page) + (let [current-file-id (:current-file-id state) + local? (= current-file-id library-id) - ldata (dsh/lookup-file-data state library-id) + ldata (dsh/lookup-file-data state library-id) + component (get-in ldata [:components component-id]) + comp-page-id (:main-instance-page component) + comp-page (dsh/get-page ldata comp-page-id) + objects (:objects comp-page) changes (-> (pcb/empty-changes it) - (cll/generate-restore-component ldata component-id library-id page objects)) + (cll/generate-restore-component ldata component-id library-id comp-page objects)) page-id (->> changes :redo-changes (keep :page-id) first) @@ -543,7 +546,8 @@ (->> changes :redo-changes (keep :frame-id))] (rx/of (dch/commit-changes changes) - (ptk/data-event :layout/update {:page-id page-id :ids frames})))))) + (when local? + (ptk/data-event :layout/update {:page-id page-id :ids frames}))))))) (defn restore-components @@ -671,7 +675,7 @@ (dch/commit-changes changes))))))) (defn go-to-component-file - [file-id component] + [file-id component update-layout?] (assert (uuid? file-id) "expected an uuid for `file-id`") (assert (ctk/check-component component) "expected a valid component") @@ -682,24 +686,34 @@ (let [params (-> (rt/get-params state) (assoc :file-id file-id) (assoc :page-id (:main-instance-page component)) - (assoc :component-id (:id component)))] + (assoc :component-id (:id component)) + (assoc :update-layout update-layout?))] (rx/of (rt/nav :workspace params ::rt/new-window true)))))) (defn go-to-local-component ;; id is the id of the component to go ;; additional-ids are ids of additional components on the same page ;; that will be selected and zoomed along the main one - [& {:keys [id additional-ids] :as options}] + ;; update-layout? indicates if it should send a :layout/update event + ;; for the parents of the component + [& {:keys [id additional-ids update-layout?] :as options}] (ptk/reify ::go-to-local-component ptk/WatchEvent (watch [_ state stream] (let [current-page-id (:current-page-id state) data (dsh/lookup-file-data state) + objects (dsh/lookup-page-objects state current-page-id) + select-and-zoom (fn [ids] - (rx/of (dws/select-shapes ids) - dwz/zoom-to-selected-shape)) + (let [parent-ids (when update-layout? + (map #(-> (get objects %) :parent-id) ids))] + (rx/of + (dws/select-shapes ids) + dwz/zoom-to-selected-shape + (when update-layout? + (ptk/data-event :layout/update {:ids parent-ids}))))) redirect-to-page (fn [page-id ids] diff --git a/frontend/src/app/main/ui/workspace/sidebar/assets/common.cljs b/frontend/src/app/main/ui/workspace/sidebar/assets/common.cljs index 3c97fa6d6d..868304fd80 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/assets/common.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/assets/common.cljs @@ -451,26 +451,28 @@ ;; When the show-remote is after a restore, the component may still be deleted do-show-remote-component - #(when-let [comp (find-component shape true)] - (st/emit! (dwl/go-to-component-file library-id comp))) + (fn [update-layout?] + (when-let [comp (find-component shape true)] + (st/emit! (dwl/go-to-component-file library-id comp update-layout?)))) do-show-component - (fn [] + (fn [_ update-layout?] (st/emit! dw/hide-context-menu) (if local-component? (do-show-local-component) - (do-show-remote-component))) + (do-show-remote-component update-layout?))) do-restore-component - #(let [;; Extract a map of component-id -> component-file in order to avoid duplicates - comps-to-restore (reduce (fn [id-file-map {:keys [component-id component-file]}] - (assoc id-file-map component-id component-file)) - {} - restorable-copies)] + (fn [] + (let [;; Extract a map of component-id -> component-file in order to avoid duplicates + comps-to-restore (reduce (fn [id-file-map {:keys [component-id component-file]}] + (assoc id-file-map component-id component-file)) + {} + restorable-copies)] - (st/emit! (dwl/restore-components comps-to-restore)) - (when (= 1 (count comps-to-restore)) - (ts/schedule 1000 do-show-component))) + (st/emit! (dwl/restore-components comps-to-restore)) + (when (= 1 (count comps-to-restore)) + (ts/schedule 1000 #(do-show-component nil true))))) menu-entries [(when (and (or (not multi) same-variant?) main-instance?) {:title (tr "workspace.shape.menu.show-in-assets") diff --git a/frontend/src/app/main/ui/workspace/sidebar/assets/components.cljs b/frontend/src/app/main/ui/workspace/sidebar/assets/components.cljs index afdd93b827..d8e81bf4ee 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/assets/components.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/assets/components.cljs @@ -87,7 +87,7 @@ (dom/stop-propagation event) (if is-local (st/emit! (dwl/go-to-local-component :id component-id)) - (st/emit! (dwl/go-to-component-file file-id component))))) + (st/emit! (dwl/go-to-component-file file-id component false))))) on-drop (mf/use-fn @@ -537,7 +537,7 @@ (if is-local (st/emit! (dwl/go-to-local-component :id current-component-id)) (let [component (d/seek #(= (:id %) current-component-id) components)] - (st/emit! (dwl/go-to-component-file file-id component)))))) + (st/emit! (dwl/go-to-component-file file-id component false)))))) on-asset-click (mf/use-fn (mf/deps groups on-asset-click) (partial on-asset-click groups)) diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/component.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/component.cljs index c559743f70..aaa8d808a1 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/component.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/component.cljs @@ -433,7 +433,8 @@ (st/emit! (dwl/go-to-local-component {:id (first ids) :additional-ids (rest ids)})) (st/emit! (dwl/go-to-component-file (:component-file shape) - (first duplicated-comps)))))) + (first duplicated-comps) + false))))) select-malformed-comps (mf/use-fn @@ -442,7 +443,7 @@ (let [ids (map :id malformed-comps)] (if (= current-file-id (:component-file shape)) (st/emit! (dwl/go-to-local-component :id (first ids) :additional-ids (rest ids))) - (st/emit! (dwl/go-to-component-file (:component-file shape) (first malformed-comps))))))) + (st/emit! (dwl/go-to-component-file (:component-file shape) (first malformed-comps) false)))))) switch-component (mf/use-fn