0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-11 23:31:21 -05:00

Merge pull request #4661 from penpot/hiru-improve-slot-validation

🐛 Add validate and repair for :misplaced-slot
This commit is contained in:
Pablo Alba 2024-06-03 16:08:54 +02:00 committed by GitHub
commit 928fec0903
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 54 additions and 3 deletions

View file

@ -6,4 +6,4 @@
(ns app.common.files.defaults)
(def version 47)
(def version 48)

View file

@ -923,6 +923,20 @@
(-> data
(update :pages-index update-vals update-page))))
(defn migrate-up-48
[data]
(letfn [(fix-shape [shape]
(let [swap-slot (ctk/get-swap-slot shape)]
(if (and (some? swap-slot)
(not (ctk/subcopy-head? shape)))
(ctk/remove-swap-slot shape)
shape)))
(update-page [page]
(d/update-when page :objects update-vals fix-shape))]
(-> data
(update :pages-index update-vals update-page))))
(def migrations
"A vector of all applicable migrations"
[{:id 2 :migrate-up migrate-up-2}
@ -961,4 +975,5 @@
{:id 44 :migrate-up migrate-up-44}
{:id 45 :migrate-up migrate-up-45}
{:id 46 :migrate-up migrate-up-46}
{:id 47 :migrate-up migrate-up-47}])
{:id 47 :migrate-up migrate-up-47}
{:id 48 :migrate-up migrate-up-48}])

View file

@ -460,6 +460,19 @@
(pcb/with-library-data file-data)
(pcb/update-component (:id shape) repair-component))))
(defmethod repair-error :misplaced-slot
[_ {:keys [shape page-id] :as error} file-data _]
(let [repair-shape
(fn [shape]
;; Remove the swap slot
(log/debug :hint (str " -> remove swap-slot"))
(ctk/remove-swap-slot shape))]
(log/dbg :hint "repairing shape :misplaced-slot" :id (:id shape) :name (:name shape) :page-id page-id)
(-> (pcb/empty-changes nil page-id)
(pcb/with-file-data file-data)
(pcb/update-shapes [(:id shape)] repair-shape))))
(defmethod repair-error :missing-slot
[_ {:keys [shape page-id args] :as error} file-data _]
(let [repair-shape

View file

@ -52,6 +52,7 @@
:not-component-not-allowed
:component-nil-objects-not-allowed
:instance-head-not-frame
:misplaced-slot
:missing-slot})
(def ^:private
@ -287,6 +288,14 @@
"Shape inside main instance should not have shape-ref"
shape file page)))
(defn- check-empty-swap-slot
"Validate that this shape does not have any swap slot."
[shape file page]
(when (some? (ctk/get-swap-slot shape))
(report-error :misplaced-slot
"This shape should not have swap slot"
shape file page)))
(defn- check-shape-main-root-top
"Root shape of a top main instance:
@ -298,6 +307,7 @@
(check-component-main-head shape file page libraries)
(check-component-root shape file page)
(check-component-not-ref shape file page)
(check-empty-swap-slot shape file page)
(run! #(check-shape % file page libraries :context :main-top) (:shapes shape)))
(defn- check-shape-main-root-nested
@ -309,6 +319,7 @@
(check-component-main-head shape file page libraries)
(check-component-not-root shape file page)
(check-component-not-ref shape file page)
(check-empty-swap-slot shape file page)
(run! #(check-shape % file page libraries :context :main-nested) (:shapes shape)))
(defn- check-shape-copy-root-top
@ -323,6 +334,7 @@
(check-component-not-main-head shape file page libraries)
(check-component-root shape file page)
(check-component-ref shape file page libraries)
(check-empty-swap-slot shape file page)
(run! #(check-shape % file page libraries :context :copy-top :library-exists library-exists) (:shapes shape))))
(defn- check-shape-copy-root-nested
@ -345,6 +357,7 @@
(check-component-not-main-not-head shape file page)
(check-component-not-root shape file page)
(check-component-not-ref shape file page)
(check-empty-swap-slot shape file page)
(run! #(check-shape % file page libraries :context :main-any) (:shapes shape)))
(defn- check-shape-copy-not-root
@ -353,6 +366,7 @@
(check-component-not-main-not-head shape file page)
(check-component-not-root shape file page)
(check-component-ref shape file page libraries)
(check-empty-swap-slot shape file page)
(run! #(check-shape % file page libraries :context :copy-any) (:shapes shape)))
(defn- check-shape-not-component
@ -362,6 +376,7 @@
(check-component-not-main-not-head shape file page)
(check-component-not-root shape file page)
(check-component-not-ref shape file page)
(check-empty-swap-slot shape file page)
(run! #(check-shape % file page libraries :context :not-component) (:shapes shape)))
(defn- check-shape

View file

@ -130,6 +130,15 @@
(and (some? (:component-id shape))
(nil? (:component-root shape))))
(defn subcopy-head?
"Check if this shape is the head of a subinstance that is a copy."
[shape]
;; This is redundant with the previous one, but may give more security
;; in case of bugs.
(and (some? (:component-id shape))
(nil? (:component-root shape))
(some? (:shape-ref shape))))
(defn instance-of?
[shape file-id component-id]
(and (some? (:component-id shape))
@ -227,7 +236,6 @@
:shape-ref
:touched))
(defn- extract-ids [shape]
(if (map? shape)
(let [current-id (:id shape)