diff --git a/common/src/app/common/types/container.cljc b/common/src/app/common/types/container.cljc index 057e86d8d..8e9a0de78 100644 --- a/common/src/app/common/types/container.cljc +++ b/common/src/app/common/types/container.cljc @@ -94,7 +94,7 @@ (map #(get-shape container %) (:shapes shape))) (defn get-component-shape - "Get the parent shape linked to a component for this shape, if any" + "Get the parent top shape linked to a component for this shape, if any" ([objects shape] (get-component-shape objects shape nil)) ([objects shape {:keys [allow-main?] :or {allow-main? false} :as options}] (cond @@ -113,6 +113,26 @@ :else (get-component-shape objects (get objects (:parent-id shape)) options)))) +(defn get-head-shape + "Get the parent top or nested shape linked to a component for this shape, if any" + ([objects shape] (get-head-shape objects shape nil)) + ([objects shape {:keys [allow-main?] :or {allow-main? false} :as options}] + (cond + (nil? shape) + nil + + (cph/root? shape) + nil + + (ctk/instance-head? shape) + shape + + (and (not (ctk/in-component-copy? shape)) (not allow-main?)) + nil + + :else + (get-head-shape objects (get objects (:parent-id shape)) options)))) + (defn get-instance-root "Get the parent shape at the top of the component instance (main or copy)." [objects shape] diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 5e5c4307a..9a25dcd8d 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -177,15 +177,26 @@ (defn find-ref-shape "Locate the near component in the local file or libraries, and retrieve the shape referenced by the instance shape." - [file page libraries shape] + [file page libraries shape & {:keys [include-deleted?] :or {include-deleted? false}}] (let [root-shape (ctn/get-component-shape (:objects page) shape) component-file (if (= (:component-file root-shape) (:id file)) file (get libraries (:component-file root-shape))) component (when component-file - (ctkl/get-component (:data component-file) (:component-id root-shape)))] - (when component - (get-ref-shape (:data component-file) component shape)))) + (ctkl/get-component (:data component-file) (:component-id root-shape) include-deleted?)) + ref-shape (when component + (get-ref-shape (:data component-file) component shape))] + + (if (some? ref-shape) ; There is a case when we have a nested orphan copy. In this case there is no near + ref-shape ; component for this copy, so shape-ref points to the remote main. + (let [head-shape (ctn/get-head-shape (:objects page) shape) + head-file (if (= (:component-file head-shape) (:id file)) + file + (get libraries (:component-file head-shape))) + head-component (when (some? head-file) + (ctkl/get-component (:data head-file) (:component-id head-shape) include-deleted?))] + (when (some? head-component) + (get-ref-shape (:data head-file) head-component shape)))))) (defn find-remote-shape "Recursively go back by the :shape-ref of the shape until find the correct shape of the original component"