From 74d2273d24c4138d0d86b1657f7eddc8c22913ca Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Wed, 28 Feb 2024 13:24:45 +0100 Subject: [PATCH] :bug: Fix problems on sync with components chain with deleted components --- common/dev/user.clj | 2 +- common/src/app/common/files/changes.cljc | 30 ++++++++++--------- common/src/app/common/types/file.cljc | 6 ++++ .../data/workspace/libraries_helpers.cljs | 12 ++++---- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/common/dev/user.clj b/common/dev/user.clj index e73de3a90..3cb650922 100644 --- a/common/dev/user.clj +++ b/common/dev/user.clj @@ -6,11 +6,11 @@ (ns user (:require + [app.common.pprint :as pp] [app.common.schema :as sm] [app.common.schema.desc-js-like :as smdj] [app.common.schema.desc-native :as smdn] [app.common.schema.generators :as sg] - [app.common.pprint :as pp] [clojure.java.io :as io] [clojure.pprint :refer [pprint print-table]] [clojure.repl :refer :all] diff --git a/common/src/app/common/files/changes.cljc b/common/src/app/common/files/changes.cljc index c87e43368..2f1858505 100644 --- a/common/src/app/common/files/changes.cljc +++ b/common/src/app/common/files/changes.cljc @@ -749,21 +749,23 @@ (defmulti components-changed (fn [_ change] (:type change))) (defmethod components-changed :mod-obj - [file-data {:keys [id page-id _component-id operations]}] - (when page-id - (let [page (ctpl/get-page file-data page-id) - shape-and-parents (map #(ctn/get-shape page %) - (cons id (cfh/get-parent-ids (:objects page) id))) - need-sync? (fn [operation] - ; We need to trigger a sync if the shape has changed any - ; attribute that participates in components synchronization. - (and (= (:type operation) :set) - (get ctk/sync-attrs (:attr operation)))) - any-sync? (some need-sync? operations)] - (when any-sync? - (let [xform (comp (filter :main-instance) ; Select shapes that are main component instances + [file-data {:keys [id page-id component-id operations]}] + (let [need-sync? (fn [operation] + ; We need to trigger a sync if the shape has changed any + ; attribute that participates in components synchronization. + (and (= (:type operation) :set) + (get ctk/sync-attrs (:attr operation)))) + any-sync? (some need-sync? operations)] + (when any-sync? + (if page-id + (let [page (ctpl/get-page file-data page-id) + shape-and-parents (map #(ctn/get-shape page %) + (cons id (cfh/get-parent-ids (:objects page) id))) + xform (comp (filter :main-instance) ; Select shapes that are main component instances (map :component-id))] - (into #{} xform shape-and-parents)))))) + (into #{} xform shape-and-parents)) + (when component-id + #{component-id}))))) (defmethod components-changed :mov-objects [file-data {:keys [page-id _component-id parent-id shapes] :as change}] diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 9fe6831cf..58b579f2e 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -103,6 +103,12 @@ (concat (map #(ctn/make-container % :page) (ctpl/pages-seq file-data)) (map #(ctn/make-container % :component) (ctkl/components-seq file-data)))) +(defn object-containers-seq + "Generate a sequence of all pages and all deleted components (all those components that have :objects), wrapped as containers" + [file-data] + (concat (map #(ctn/make-container % :page) (ctpl/pages-seq file-data)) + (map #(ctn/make-container % :component) (ctkl/deleted-components-seq file-data)))) + (defn update-container "Update a container inside the file, it can be a page or a component" [file-data container f] diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index 84b429de4..32152b2ba 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -317,10 +317,10 @@ (let [file (wsh/get-file state file-id) components-v2 (get-in file [:options :components-v2])] - (loop [pages (vals (get file :pages-index)) + (loop [containers (ctf/object-containers-seq file) changes (pcb/empty-changes it)] - (if-let [page (first pages)] - (recur (next pages) + (if-let [container (first containers)] + (recur (next containers) (pcb/concat-changes changes (generate-sync-container it @@ -328,7 +328,7 @@ asset-id library-id state - (cfh/make-container page :page) + container components-v2))) changes)))) @@ -597,9 +597,7 @@ (log/debug :msg "Sync shape direct" :shape (str shape-id) :reset? reset?) (let [shape-inst (ctn/get-shape container shape-id) library (dm/get-in libraries [(:component-file shape-inst) :data]) - component (or (ctkl/get-component library (:component-id shape-inst)) - (and reset? - (ctkl/get-deleted-component library (:component-id shape-inst))))] + component (ctkl/get-component library (:component-id shape-inst) true)] (if (and (ctk/in-component-copy? shape-inst) (or (ctf/direct-copy? shape-inst component container nil libraries) reset?)) ; In a normal sync, we don't want to sync remote mains, only direct/near (let [redirect-shaperef (partial redirect-shaperef container libraries)