0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-29 08:01:41 -05:00
This commit is contained in:
Andrés Moya 2024-10-17 16:51:18 +02:00
parent c2ba7cdbc7
commit c239b5868e
3 changed files with 43 additions and 21 deletions

View file

@ -276,12 +276,19 @@
"Validate that the referenced shape exists in the near component." "Validate that the referenced shape exists in the near component."
[shape file page libraries] [shape file page libraries]
(let [library-exists (library-exists? file libraries shape) (let [library-exists (library-exists? file libraries shape)
has-swap-slot? (some? (ctk/get-swap-slot shape))
ref-shape (when library-exists ref-shape (when library-exists
(ctf/find-ref-shape file page libraries shape :include-deleted? true))] (ctf/find-ref-shape file page libraries shape
:include-deleted? true
:only-lower? has-swap-slot?))]
(when (and library-exists (nil? ref-shape)) (when (and library-exists (nil? ref-shape))
(report-error :ref-shape-not-found (if has-swap-slot?
(str/ffmt "Referenced shape % not found in near component" (:shape-ref shape)) (report-error :ref-shape-not-found
shape file page)))) (str/ffmt "Referenced shape % not found in near component" (:shape-ref shape))
shape file page)
(report-error :ref-shape-not-found
(str/ffmt "Referenced shape % not found in component" (:shape-ref shape))
shape file page)))))
(defn- check-component-not-ref (defn- check-component-not-ref
"Validate that this shape does not reference other one." "Validate that this shape does not reference other one."
@ -364,13 +371,16 @@
- :component-file - :component-file
- :shape-ref" - :shape-ref"
[shape file page libraries library-exists] [shape file page libraries library-exists]
(check-component-not-main-head shape file page libraries) (let [library-exists (or library-exists (library-exists? file libraries shape))]
(check-component-not-root shape file page) (when (= (:id shape) #uuid "81f32503-7638-8059-8005-1883e6d1a4fa")
;; We can have situations where the nested copy and the ancestor copy come from different libraries and some of them have been dettached (prn "ok1" library-exists))
;; so we only validate the shape-ref if the ancestor is from a valid library (check-component-not-main-head shape file page libraries)
(when library-exists (check-component-not-root shape file page)
(check-component-ref shape file page libraries)) ;; We can have situations where the nested copy and the ancestor copy come from different libraries and some of them have been dettached
(run! #(check-shape % file page libraries :context :copy-nested) (:shapes shape))) ;; so we only validate the shape-ref if the ancestor is from a valid library
(when library-exists
(check-component-ref shape file page libraries))
(run! #(check-shape % file page libraries :context :copy-nested) (:shapes shape))))
(defn- check-shape-main-not-root (defn- check-shape-main-not-root
"Not-root shape of a main instance (not any attribute)" "Not-root shape of a main instance (not any attribute)"

View file

@ -259,20 +259,24 @@
(defn- generate-detach-recursive (defn- generate-detach-recursive
[changes container libraries shape-id first component-root?] [changes container libraries shape-id first component-root?]
(let [shape (ctn/get-shape container shape-id)] (let [shape (ctn/get-shape container shape-id)]
(prn "shape-id" (:id shape))
(if (and (ctk/instance-head? shape) (not first)) (if (and (ctk/instance-head? shape) (not first))
; Subinstances are not detached ; Subinstances are not detached
(cond-> changes (do
component-root? (when (= (:id shape) #uuid "81f32503-7638-8059-8005-1883e6d1a4fa")
(prn "wurxt" (ctk/get-swap-slot shape)))
(cond-> changes
component-root?
; If the initial shape was component-root, first level subinstances are converted in top instances ; If the initial shape was component-root, first level subinstances are converted in top instances
(pcb/update-shapes [shape-id] #(assoc % :component-root true)) (pcb/update-shapes [shape-id] #(assoc % :component-root true))
:always :always
; First level subinstances of a detached component can't have swap-slot ; First level subinstances of a detached component can't have swap-slot
(pcb/update-shapes [shape-id] ctk/remove-swap-slot) (pcb/update-shapes [shape-id] ctk/remove-swap-slot)
(nil? (ctk/get-swap-slot shape)) (nil? (ctk/get-swap-slot shape))
; Near shape-refs need to be advanced one level (except if the head is already swapped) ; Near shape-refs need to be advanced one level (except if the head is already swapped)
(generate-advance-nesting-level nil container libraries (:id shape))) (generate-advance-nesting-level nil container libraries (:id shape))))
;; Otherwise, detach the shape and all children ;; Otherwise, detach the shape and all children
(let [children-ids (:shapes shape)] (let [children-ids (:shapes shape)]

View file

@ -262,16 +262,24 @@
(defn find-ref-shape (defn find-ref-shape
"Locate the nearest component in the local file or libraries, and retrieve the shape "Locate the nearest component in the local file or libraries, and retrieve the shape
referenced by the instance shape." referenced by the instance shape."
[file container libraries shape & {:keys [include-deleted? with-context?] :or {include-deleted? false with-context? false}}] [file container libraries shape & {:keys [include-deleted? with-context? only-lower?]
:or {include-deleted? false with-context? false only-lower? false}}]
(let [find-ref-shape-in-head (let [find-ref-shape-in-head
(fn [head-shape] (fn [head-shape]
(let [component-file (find-component-file file libraries (:component-file head-shape)) (let [component-file (find-component-file file libraries (:component-file head-shape))
component (when (some? component-file) component (when (some? component-file)
(ctkl/get-component (:data component-file) (:component-id head-shape) include-deleted?))] (ctkl/get-component (:data component-file) (:component-id head-shape) include-deleted?))]
(when (some? component) (when (some? component)
(get-ref-shape (:data component-file) component shape :with-context? with-context?))))] (get-ref-shape (:data component-file) component shape :with-context? with-context?))))
parent-heads (ctn/get-parent-heads (:objects container) shape)]
;; (when (= (:id shape) #uuid "805fa3b6-a357-808b-8004-58bccaa27dca")
;; (js/console.log "shape" (clj->js shape))
;; (js/console.log "parent-heads" (clj->js parent-heads)))
(some find-ref-shape-in-head (ctn/get-parent-heads (:objects container) shape)))) (if only-lower?
(find-ref-shape-in-head (last parent-heads))
(some find-ref-shape-in-head parent-heads))))
(defn advance-shape-ref (defn advance-shape-ref
"Get the shape-ref of the near main of the shape, recursively repeated as many times "Get the shape-ref of the near main of the shape, recursively repeated as many times