mirror of
https://github.com/penpot/penpot.git
synced 2025-05-12 10:16:37 +02:00
🐛 Add script for fix files with bad shape-ref
This commit is contained in:
parent
228b09c340
commit
1bb3a3a084
5 changed files with 90 additions and 20 deletions
|
@ -7,8 +7,14 @@
|
||||||
(ns app.srepl.fixes
|
(ns app.srepl.fixes
|
||||||
"A collection of adhoc fixes scripts."
|
"A collection of adhoc fixes scripts."
|
||||||
(:require
|
(:require
|
||||||
|
[app.common.data :as d]
|
||||||
|
[app.common.exceptions :as ex]
|
||||||
[app.common.logging :as l]
|
[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.common.uuid :as uuid]
|
||||||
|
[app.rpc.commands.files :as files]
|
||||||
[app.srepl.helpers :as h]))
|
[app.srepl.helpers :as h]))
|
||||||
|
|
||||||
(defn repair-orphaned-shapes
|
(defn repair-orphaned-shapes
|
||||||
|
@ -72,4 +78,65 @@
|
||||||
|
|
||||||
([file state]
|
([file state]
|
||||||
(rename-layout-attrs file)
|
(rename-layout-attrs file)
|
||||||
(update state :total (fnil inc 0))))
|
(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))))
|
||||||
|
|
|
@ -112,13 +112,15 @@
|
||||||
|
|
||||||
;; Asset helpers
|
;; 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
|
(defn get-component
|
||||||
"Retrieve a component from libraries, if no library-id is provided, we
|
"Retrieve a component from a library."
|
||||||
iterate over all libraries and find the component on it."
|
[libraries library-id component-id & {:keys [included-delete?] :or {included-delete? false}}]
|
||||||
([libraries component-id]
|
(ctkl/get-component (dm/get-in libraries [library-id :data]) component-id included-delete?))
|
||||||
(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)))
|
|
||||||
|
|
||||||
(defn get-component-library
|
(defn get-component-library
|
||||||
"Retrieve the library the component belongs to."
|
"Retrieve the library the component belongs to."
|
||||||
|
@ -170,7 +172,8 @@
|
||||||
"Retrieve all shapes of the component"
|
"Retrieve all shapes of the component"
|
||||||
[file-data component]
|
[file-data component]
|
||||||
(let [components-v2 (dm/get-in file-data [:options :components-v2])]
|
(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)]
|
(let [instance-page (get-component-page file-data component)]
|
||||||
(cph/get-children-with-self (:objects instance-page) (:main-instance-id component)))
|
(cph/get-children-with-self (:objects instance-page) (:main-instance-id component)))
|
||||||
(vals (:objects component)))))
|
(vals (:objects component)))))
|
||||||
|
|
|
@ -82,7 +82,7 @@
|
||||||
[page root-inst-id libraries]
|
[page root-inst-id libraries]
|
||||||
(let [root-inst (ctn/get-shape page root-inst-id)
|
(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-inst (cph/get-children-with-self (:objects page) root-inst-id)
|
||||||
shapes-main (cph/get-children-with-self (:objects component) (:shape-ref root-inst))
|
shapes-main (cph/get-children-with-self (:objects component) (:shape-ref root-inst))
|
||||||
|
@ -94,7 +94,7 @@
|
||||||
(ctn/get-component-shape (:objects page) shape)
|
(ctn/get-component-shape (:objects page) shape)
|
||||||
|
|
||||||
component
|
component
|
||||||
(ctf/get-component libraries (:component-id component-shape))
|
(ctf/find-component libraries (:component-id component-shape))
|
||||||
|
|
||||||
main-shape
|
main-shape
|
||||||
(ctn/get-shape component (:shape-ref shape))]
|
(ctn/get-shape component (:shape-ref shape))]
|
||||||
|
@ -118,7 +118,7 @@
|
||||||
[page root-inst-id libraries]
|
[page root-inst-id libraries]
|
||||||
(let [root-inst (ctn/get-shape page root-inst-id)
|
(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-inst (cph/get-children-with-self (:objects page) root-inst-id)
|
||||||
shapes-main (cph/get-children-with-self (:objects component) (:shape-ref root-inst))
|
shapes-main (cph/get-children-with-self (:objects component) (:shape-ref root-inst))
|
||||||
|
@ -130,7 +130,7 @@
|
||||||
(ctn/get-component-shape (:objects page) shape)
|
(ctn/get-component-shape (:objects page) shape)
|
||||||
|
|
||||||
component
|
component
|
||||||
(ctf/get-component libraries (:component-id component-shape))
|
(ctf/find-component libraries (:component-id component-shape))
|
||||||
|
|
||||||
main-shape
|
main-shape
|
||||||
(ctn/get-shape component (:shape-ref shape))]
|
(ctn/get-shape component (:shape-ref shape))]
|
||||||
|
@ -145,7 +145,7 @@
|
||||||
(defn resolve-component
|
(defn resolve-component
|
||||||
"Get the component with the given id and all its shapes."
|
"Get the component with the given id and all its shapes."
|
||||||
[page component-id libraries]
|
[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)
|
root-main (ctk/get-component-root component)
|
||||||
shapes-main (cph/get-children-with-self (:objects component) (:id root-main))]
|
shapes-main (cph/get-children-with-self (:objects component) (:id root-main))]
|
||||||
|
|
||||||
|
|
|
@ -2291,7 +2291,6 @@
|
||||||
;; Components
|
;; Components
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
|
||||||
(defn find-components-norefs
|
(defn find-components-norefs
|
||||||
[]
|
[]
|
||||||
(ptk/reify ::find-components-norefs
|
(ptk/reify ::find-components-norefs
|
||||||
|
@ -2300,7 +2299,8 @@
|
||||||
(let [objects (wsh/lookup-page-objects state)
|
(let [objects (wsh/lookup-page-objects state)
|
||||||
copies (->> objects
|
copies (->> objects
|
||||||
vals
|
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)
|
copies-no-ref (filter #(not (:shape-ref %)) copies)
|
||||||
find-childs-no-ref (fn [acc-map item]
|
find-childs-no-ref (fn [acc-map item]
|
||||||
(let [id (:id item)
|
(let [id (:id item)
|
||||||
|
@ -2313,8 +2313,8 @@
|
||||||
find-childs-no-ref
|
find-childs-no-ref
|
||||||
{}
|
{}
|
||||||
copies)]
|
copies)]
|
||||||
(js/console.log "Copies no ref" (clj->js copies-no-ref))
|
(js/console.log "Copies no ref" (count copies-no-ref) (clj->js copies-no-ref))
|
||||||
(js/console.log "Childs no ref" (clj->js childs-no-ref))))))
|
(js/console.log "Childs no ref" (count childs-no-ref) (clj->js childs-no-ref))))))
|
||||||
|
|
||||||
(defn set-shape-ref
|
(defn set-shape-ref
|
||||||
[id shape-ref]
|
[id shape-ref]
|
||||||
|
|
|
@ -112,7 +112,7 @@
|
||||||
main-instance? (:main-instance root-inst)
|
main-instance? (:main-instance root-inst)
|
||||||
|
|
||||||
libs (wsh/get-libraries state)
|
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)
|
library (ctf/get-component-library libs root-inst)
|
||||||
|
|
||||||
shapes-inst (cph/get-children-with-self (:objects page) root-inst-id)
|
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)
|
root-inst (ctn/get-shape page root-inst-id)
|
||||||
|
|
||||||
libs (wsh/get-libraries state)
|
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)
|
library (ctf/get-component-library libs root-inst)
|
||||||
|
|
||||||
shapes-inst (cph/get-children-with-self (:objects page) root-inst-id)
|
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."
|
"Get the component with the given id and all its shapes."
|
||||||
[state component-id]
|
[state component-id]
|
||||||
(let [libs (wsh/get-libraries state)
|
(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)
|
library (ctf/get-component-library libs component)
|
||||||
shapes-main (ctf/get-component-shapes (:data library) component)]
|
shapes-main (ctf/get-component-shapes (:data library) component)]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue