diff --git a/backend/src/app/srepl/fixes.clj b/backend/src/app/srepl/fixes.clj index a294a25d3..4f8b4558f 100644 --- a/backend/src/app/srepl/fixes.clj +++ b/backend/src/app/srepl/fixes.clj @@ -7,8 +7,14 @@ (ns app.srepl.fixes "A collection of adhoc fixes scripts." (:require + [app.common.data :as d] + [app.common.exceptions :as ex] [app.common.logging :as l] + [app.common.pages.helpers :as cph] + [app.common.types.component :as ctk] + [app.common.types.file :as ctf] [app.common.uuid :as uuid] + [app.rpc.commands.files :as files] [app.srepl.helpers :as h])) (defn repair-orphaned-shapes @@ -72,4 +78,65 @@ ([file state] (rename-layout-attrs file) - (update state :total (fnil inc 0)))) \ No newline at end of file + (update state :total (fnil inc 0)))) + +(defn fix-components-shaperefs + [file] + (if-not (contains? (:features file) "components/v2") + (ex/raise :type :invalid-file + :code :invalid-file + :hint "this file is not v2") + (let [libs (->> (files/get-file-libraries app.srepl.helpers/*conn* (:id file)) + (cons file) + (map #(files/get-file app.srepl.helpers/*conn* (:id %) (:features file))) + (d/index-by :id)) + + fix-copy-item + (fn fix-copy-item [allow-head shapes-copy shapes-base copy-id base-id] + (let [copy (first (filter #(= (:id %) copy-id) shapes-copy)) + ;; do nothing if it is a copy inside of a copy. It will be treated later + stop? (and (not allow-head) (ctk/instance-head? copy)) + base (first (filter #(= (:id %) base-id) shapes-base)) + fci (partial fix-copy-item false shapes-copy shapes-base) + + updates (if (and + (not stop?) + (not= (:shape-ref copy) base-id)) + [[(:id copy) base-id]] + []) + + child-updates (if (and + (not stop?) + ;; If the base has the same number of childrens than the copy, we asume + ;; that the shaperefs can be fixed ad pointed in the same order + (= (count (:shapes copy)) (count (:shapes base)))) + (apply concat (mapv fci (:shapes copy) (:shapes base))) + [])] + (concat updates child-updates))) + + fix-copy + (fn [objects updates copy] + (let [component (ctf/find-component libs (:component-id copy) {:included-delete? true}) + component-file (get libs (:component-file copy)) + component-shapes (ctf/get-component-shapes (:data component-file) component) + copy-shapes (cph/get-children-with-self objects (:id copy)) + + copy-updates (fix-copy-item true copy-shapes component-shapes (:id copy) (:main-instance-id component))] + (concat updates copy-updates))) + + update-page (fn [page] + (let [objects (:objects page) + fc (partial fix-copy objects) + copies (->> objects + vals + (filter #(and (ctk/instance-head? %) (not (ctk/main-instance? %))))) + updates (reduce fc [] copies) + updated-page (reduce (fn [p [id shape-ref]] + (assoc-in p [:objects id :shape-ref] shape-ref)) + page + updates)] + (prn (str "Page " (:name page) " - Fixing " (count updates))) + updated-page))] + + (prn (str "Updating " (:name file) " " (:id file))) + (update file :data h/update-pages update-page)))) diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 05bdd6335..b33dd899f 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -112,13 +112,15 @@ ;; Asset helpers +(defn find-component + "Retrieve a component from libraries, iterating over all of them." + [libraries component-id & {:keys [included-delete?] :or {included-delete? false}}] + (some #(ctkl/get-component (:data %) component-id included-delete?) (vals libraries))) + (defn get-component - "Retrieve a component from libraries, if no library-id is provided, we - iterate over all libraries and find the component on it." - ([libraries component-id] - (some #(ctkl/get-component (:data %) component-id) (vals libraries))) - ([libraries library-id component-id] - (ctkl/get-component (dm/get-in libraries [library-id :data]) component-id))) + "Retrieve a component from a library." + [libraries library-id component-id & {:keys [included-delete?] :or {included-delete? false}}] + (ctkl/get-component (dm/get-in libraries [library-id :data]) component-id included-delete?)) (defn get-component-library "Retrieve the library the component belongs to." @@ -170,7 +172,8 @@ "Retrieve all shapes of the component" [file-data component] (let [components-v2 (dm/get-in file-data [:options :components-v2])] - (if components-v2 + (if (and components-v2 + (not (:deleted component))) ;; the deleted components have its children in the :objects property (let [instance-page (get-component-page file-data component)] (cph/get-children-with-self (:objects instance-page) (:main-instance-id component))) (vals (:objects component))))) diff --git a/common/test/common_tests/helpers/components.cljc b/common/test/common_tests/helpers/components.cljc index 4cfbdd547..56f59af34 100644 --- a/common/test/common_tests/helpers/components.cljc +++ b/common/test/common_tests/helpers/components.cljc @@ -82,7 +82,7 @@ [page root-inst-id libraries] (let [root-inst (ctn/get-shape page root-inst-id) - component (ctf/get-component libraries (:component-id root-inst)) + component (ctf/find-component libraries (:component-id root-inst)) shapes-inst (cph/get-children-with-self (:objects page) root-inst-id) shapes-main (cph/get-children-with-self (:objects component) (:shape-ref root-inst)) @@ -94,7 +94,7 @@ (ctn/get-component-shape (:objects page) shape) component - (ctf/get-component libraries (:component-id component-shape)) + (ctf/find-component libraries (:component-id component-shape)) main-shape (ctn/get-shape component (:shape-ref shape))] @@ -118,7 +118,7 @@ [page root-inst-id libraries] (let [root-inst (ctn/get-shape page root-inst-id) - component (ctf/get-component libraries (:component-id root-inst)) + component (ctf/find-component libraries (:component-id root-inst)) shapes-inst (cph/get-children-with-self (:objects page) root-inst-id) shapes-main (cph/get-children-with-self (:objects component) (:shape-ref root-inst)) @@ -130,7 +130,7 @@ (ctn/get-component-shape (:objects page) shape) component - (ctf/get-component libraries (:component-id component-shape)) + (ctf/find-component libraries (:component-id component-shape)) main-shape (ctn/get-shape component (:shape-ref shape))] @@ -145,7 +145,7 @@ (defn resolve-component "Get the component with the given id and all its shapes." [page component-id libraries] - (let [component (ctf/get-component libraries component-id) + (let [component (ctf/find-component libraries component-id) root-main (ctk/get-component-root component) shapes-main (cph/get-children-with-self (:objects component) (:id root-main))] diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index f4aba631f..0b4df68a5 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -2291,7 +2291,6 @@ ;; Components ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - (defn find-components-norefs [] (ptk/reify ::find-components-norefs @@ -2300,7 +2299,8 @@ (let [objects (wsh/lookup-page-objects state) copies (->> objects vals - (filter #(and (:component-root %) (not (:main-instance %))))) + (filter #(and (ctk/instance-head? %) (not (ctk/main-instance? %))))) + copies-no-ref (filter #(not (:shape-ref %)) copies) find-childs-no-ref (fn [acc-map item] (let [id (:id item) @@ -2313,8 +2313,8 @@ find-childs-no-ref {} copies)] - (js/console.log "Copies no ref" (clj->js copies-no-ref)) - (js/console.log "Childs no ref" (clj->js childs-no-ref)))))) + (js/console.log "Copies no ref" (count copies-no-ref) (clj->js copies-no-ref)) + (js/console.log "Childs no ref" (count childs-no-ref) (clj->js childs-no-ref)))))) (defn set-shape-ref [id shape-ref] diff --git a/frontend/test/frontend_tests/helpers/libraries.cljs b/frontend/test/frontend_tests/helpers/libraries.cljs index 2dd021c29..42c89b1b3 100644 --- a/frontend/test/frontend_tests/helpers/libraries.cljs +++ b/frontend/test/frontend_tests/helpers/libraries.cljs @@ -112,7 +112,7 @@ main-instance? (:main-instance root-inst) libs (wsh/get-libraries state) - component (ctf/get-component libs (:component-id root-inst)) + component (ctf/find-component libs (:component-id root-inst)) library (ctf/get-component-library libs root-inst) shapes-inst (cph/get-children-with-self (:objects page) root-inst-id) @@ -152,7 +152,7 @@ root-inst (ctn/get-shape page root-inst-id) libs (wsh/get-libraries state) - component (ctf/get-component libs (:component-id root-inst)) + component (ctf/find-component libs (:component-id root-inst)) library (ctf/get-component-library libs root-inst) shapes-inst (cph/get-children-with-self (:objects page) root-inst-id) @@ -167,7 +167,7 @@ "Get the component with the given id and all its shapes." [state component-id] (let [libs (wsh/get-libraries state) - component (ctf/get-component libs component-id) + component (ctf/find-component libs component-id) library (ctf/get-component-library libs component) shapes-main (ctf/get-component-shapes (:data library) component)]