0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-12 18:18:24 -05:00

🐛 Relax file validation for anidated components from other files

This commit is contained in:
Alejandro Alonso 2024-03-22 16:29:30 +01:00
parent 8585e73c0f
commit f66f168a99

View file

@ -69,6 +69,11 @@
(def ^:dynamic ^:private *errors* nil) (def ^:dynamic ^:private *errors* nil)
(defn- library-exists?
[file libraries shape]
(or (= (:component-file shape) (:id file))
(contains? libraries (:component-file shape))))
(defn- report-error (defn- report-error
[code hint shape file page & {:as args}] [code hint shape file page & {:as args}]
(let [error {:code code (let [error {:code code
@ -218,12 +223,11 @@
"Shape not expected to be main instance" "Shape not expected to be main instance"
shape file page)) shape file page))
(let [library-exists? (or (= (:component-file shape) (:id file)) (let [library-exists (library-exists? file libraries shape)
(contains? libraries (:component-file shape))) component (when library-exists
component (when library-exists?
(ctf/resolve-component shape file libraries {:include-deleted? true}))] (ctf/resolve-component shape file libraries {:include-deleted? true}))]
(if (nil? component) (if (nil? component)
(when library-exists? (when library-exists
(report-error :component-not-found (report-error :component-not-found
(str/ffmt "Component % not found in file %" (:component-id shape) (:component-file shape)) (str/ffmt "Component % not found in file %" (:component-id shape) (:component-file shape))
shape file page)) shape file page))
@ -265,11 +269,10 @@
(defn- check-component-ref (defn- check-component-ref
"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? (or (= (:component-file shape) (:id file)) (let [library-exists (library-exists? file libraries shape)
(contains? libraries (:component-file 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))]
(when (and library-exists? (nil? ref-shape)) (when (and library-exists (nil? ref-shape))
(report-error :ref-shape-not-found (report-error :ref-shape-not-found
(str/ffmt "Referenced shape % not found in near component" (:shape-ref shape)) (str/ffmt "Referenced shape % not found in near component" (:shape-ref shape))
shape file page)))) shape file page))))
@ -313,20 +316,25 @@
- :component-root - :component-root
- :shape-ref" - :shape-ref"
[shape file page libraries] [shape file page libraries]
(check-component-not-main-head shape file page libraries) ;; We propagate have to propagate to nested shapes if library is valid or not
(check-component-root shape file page) (let [library-exists (library-exists? file libraries shape)]
(check-component-ref shape file page libraries) (check-component-not-main-head shape file page libraries)
(run! #(check-shape % file page libraries :context :copy-top) (:shapes shape))) (check-component-root shape file page)
(check-component-ref shape file page libraries)
(run! #(check-shape % file page libraries :context :copy-top :library-exists library-exists) (:shapes shape))))
(defn- check-shape-copy-root-nested (defn- check-shape-copy-root-nested
"Root shape of a nested copy instance "Root shape of a nested copy instance
- :component-id - :component-id
- :component-file - :component-file
- :shape-ref" - :shape-ref"
[shape file page libraries] [shape file page libraries library-exists]
(check-component-not-main-head shape file page libraries) (check-component-not-main-head shape file page libraries)
(check-component-not-root shape file page) (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
;; 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))) (run! #(check-shape % file page libraries :context :copy-nested) (:shapes shape)))
(defn- check-shape-main-not-root (defn- check-shape-main-not-root
@ -367,7 +375,7 @@
- :main-any - :main-any
- :copy-any - :copy-any
" "
[shape-id file page libraries & {:keys [context] :or {context :not-component}}] [shape-id file page libraries & {:keys [context library-exists] :or {context :not-component library-exists false}}]
(let [shape (ctst/get-shape page shape-id)] (let [shape (ctst/get-shape page shape-id)]
(when (some? shape) (when (some? shape)
(check-geometry shape file page) (check-geometry shape file page)
@ -406,7 +414,7 @@
(report-error :nested-copy-not-allowed (report-error :nested-copy-not-allowed
"Nested copy component only allowed inside other component" "Nested copy component only allowed inside other component"
shape file page) shape file page)
(check-shape-copy-root-nested shape file page libraries))))) (check-shape-copy-root-nested shape file page libraries library-exists)))))
(if (ctk/in-component-copy? shape) (if (ctk/in-component-copy? shape)
(if-not (#{:copy-top :copy-nested :copy-any} context) (if-not (#{:copy-top :copy-nested :copy-any} context)