0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-26 08:29:42 -05:00

Detach instance when syncing if the master component is gone

This commit is contained in:
Andrés Moya 2021-06-28 16:00:50 +02:00 committed by Alonso Torres
parent c98958053c
commit ea8bc687c0
2 changed files with 97 additions and 94 deletions

View file

@ -27,7 +27,7 @@
[beicon.core :as rx] [beicon.core :as rx]
[potok.core :as ptk])) [potok.core :as ptk]))
;; Change this to :info :debug or :trace to debug this module ;; Change this to :info :debug or :trace to debug this module, or :warn to reset to default
(log/set-level! :warn) (log/set-level! :warn)
(defn- log-changes (defn- log-changes
@ -450,57 +450,13 @@
(ptk/reify ::detach-component (ptk/reify ::detach-component
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(let [page-id (:current-page-id state) (let [local-library (dwlh/get-local-file state)
objects (wsh/lookup-page-objects state page-id) container (cp/get-container (get state :current-page-id)
shapes (cp/get-object-with-children id objects) :page
local-library)
rchanges (mapv (fn [obj] [rchanges uchanges]
{:type :mod-obj (dwlh/generate-detach-instance id container)]
:page-id page-id
:id (:id obj)
:operations [{:type :set
:attr :component-id
:val nil}
{:type :set
:attr :component-file
:val nil}
{:type :set
:attr :component-root?
:val nil}
{:type :set
:attr :remote-synced?
:val nil}
{:type :set
:attr :shape-ref
:val nil}
{:type :set
:attr :touched
:val nil}]})
shapes)
uchanges (mapv (fn [obj]
{:type :mod-obj
:page-id page-id
:id (:id obj)
:operations [{:type :set
:attr :component-id
:val (:component-id obj)}
{:type :set
:attr :component-file
:val (:component-file obj)}
{:type :set
:attr :component-root?
:val (:component-root? obj)}
{:type :set
:attr :remote-synced?
:val (:remote-synced? obj)}
{:type :set
:attr :shape-ref
:val (:shape-ref obj)}
{:type :set
:attr :touched
:val (:touched obj)}]})
shapes)]
(rx/of (dch/commit-changes {:redo-changes rchanges (rx/of (dch/commit-changes {:redo-changes rchanges
:undo-changes uchanges :undo-changes uchanges

View file

@ -17,7 +17,7 @@
[cljs.spec.alpha :as s] [cljs.spec.alpha :as s]
[clojure.set :as set])) [clojure.set :as set]))
;; Change this to :info :debug or :trace to debug this module ;; Change this to :info :debug or :trace to debug this module, or :warn to reset to default
(log/set-level! :warn) (log/set-level! :warn)
(defonce empty-changes [[] []]) (defonce empty-changes [[] []])
@ -200,6 +200,63 @@
(get component :objects) (get component :objects)
identity))) identity)))
(defn generate-detach-instance
"Generate changes to remove the links between a shape and all its children
with a component."
[shape-id container]
(let [shapes (cp/get-object-with-children shape-id (:objects container))
rchanges (mapv (fn [obj]
(make-change
container
{:type :mod-obj
:id (:id obj)
:operations [{:type :set
:attr :component-id
:val nil}
{:type :set
:attr :component-file
:val nil}
{:type :set
:attr :component-root?
:val nil}
{:type :set
:attr :remote-synced?
:val nil}
{:type :set
:attr :shape-ref
:val nil}
{:type :set
:attr :touched
:val nil}]}))
shapes)
uchanges (mapv (fn [obj]
(make-change
container
{:type :mod-obj
:id (:id obj)
:operations [{:type :set
:attr :component-id
:val (:component-id obj)}
{:type :set
:attr :component-file
:val (:component-file obj)}
{:type :set
:attr :component-root?
:val (:component-root? obj)}
{:type :set
:attr :remote-synced?
:val (:remote-synced? obj)}
{:type :set
:attr :shape-ref
:val (:shape-ref obj)}
{:type :set
:attr :touched
:val (:touched obj)}]}))
shapes)]
[rchanges uchanges]))
;; ---- General library synchronization functions ---- ;; ---- General library synchronization functions ----
@ -216,26 +273,20 @@
:file (pretty-file file-id state) :file (pretty-file file-id state)
:library (pretty-file library-id state)) :library (pretty-file library-id state))
(let [file (get-file state file-id) (let [file (get-file state file-id)]
library (get-file state library-id) (loop [pages (vals (get file :pages-index))
library-items (get library asset-type)] rchanges []
uchanges []]
(if (empty? library-items) (if-let [page (first pages)]
empty-changes (let [[page-rchanges page-uchanges]
(generate-sync-container asset-type
(loop [pages (vals (get file :pages-index)) library-id
rchanges [] state
uchanges []] (cp/make-container page :page))]
(if-let [page (first pages)] (recur (next pages)
(let [[page-rchanges page-uchanges] (d/concat rchanges page-rchanges)
(generate-sync-container asset-type (d/concat uchanges page-uchanges)))
library-id [rchanges uchanges]))))
state
(cp/make-container page :page))]
(recur (next pages)
(d/concat rchanges page-rchanges)
(d/concat uchanges page-uchanges)))
[rchanges uchanges])))))
(defn generate-sync-library (defn generate-sync-library
"Generate changes to synchronize all shapes in all components of the "Generate changes to synchronize all shapes in all components of the
@ -248,27 +299,21 @@
:file (pretty-file file-id state) :file (pretty-file file-id state)
:library (pretty-file library-id state)) :library (pretty-file library-id state))
(let [file (get-file state file-id) (let [file (get-file state file-id)]
library (get-file state library-id) (loop [local-components (vals (get file :components))
library-items (get library asset-type)] rchanges []
uchanges []]
(if (empty? library-items) (if-let [local-component (first local-components)]
empty-changes (let [[comp-rchanges comp-uchanges]
(generate-sync-container asset-type
(loop [local-components (vals (get file :components)) library-id
rchanges [] state
uchanges []] (cp/make-container local-component
(if-let [local-component (first local-components)] :component))]
(let [[comp-rchanges comp-uchanges] (recur (next local-components)
(generate-sync-container asset-type (d/concat rchanges comp-rchanges)
library-id (d/concat uchanges comp-uchanges)))
state [rchanges uchanges]))))
(cp/make-container local-component
:component))]
(recur (next local-components)
(d/concat rchanges comp-rchanges)
(d/concat uchanges comp-uchanges)))
[rchanges uchanges])))))
(defn- generate-sync-container (defn- generate-sync-container
"Generate changes to synchronize all shapes in a particular container (a page "Generate changes to synchronize all shapes in a particular container (a page
@ -568,7 +613,9 @@
root-main root-main
reset? reset?
initial-root?) initial-root?)
empty-changes))) ; If the component is not found, because the master component has been
; deleted or the library unlinked, detach the instance.
(generate-detach-instance shape-id container))))
(defn- generate-sync-shape-direct-recursive (defn- generate-sync-shape-direct-recursive
[container shape-inst component shape-main root-inst root-main reset? initial-root?] [container shape-inst component shape-main root-inst root-main reset? initial-root?]