mirror of
https://github.com/penpot/penpot.git
synced 2025-03-18 10:41:29 -05:00
✨ Synchronize add/delete/move shapes in componentes
This commit is contained in:
parent
912be76400
commit
ebb7410e5b
5 changed files with 640 additions and 405 deletions
|
@ -251,7 +251,8 @@
|
|||
:add-media :mod-media :del-media
|
||||
:add-component :mod-component :del-component
|
||||
:add-typography :mod-typography :del-typography} (:type change))
|
||||
(and (= (:type change) :mod-obj)
|
||||
(and (#{:add-obj :mod-obj :del-obj
|
||||
:reg-objects :mov-objects} (:type change))
|
||||
(some? (:component-id change)))))
|
||||
|
||||
(declare update-file)
|
||||
|
|
|
@ -435,6 +435,14 @@
|
|||
:internal.file/recent-colors
|
||||
:internal.file/media]))
|
||||
|
||||
(s/def ::container-type #{:page :component})
|
||||
|
||||
(s/def ::container
|
||||
(s/keys :req-un [::container-type
|
||||
::id
|
||||
::name
|
||||
:internal.page/objects]))
|
||||
|
||||
(defmulti operation-spec :type)
|
||||
|
||||
(s/def :internal.operations.set/attr keyword?)
|
||||
|
@ -461,9 +469,9 @@
|
|||
(s/def :internal.changes.add-obj/obj ::shape)
|
||||
|
||||
(defmethod change-spec :add-obj [_]
|
||||
(s/keys :req-un [::id ::page-id ::frame-id
|
||||
(s/keys :req-un [::id (or ::page-id ::component-id)
|
||||
:internal.changes.add-obj/obj]
|
||||
:opt-un [::parent-id]))
|
||||
:opt-un [::parent-id ::frame-id]))
|
||||
|
||||
(s/def ::operation (s/multi-spec operation-spec :type))
|
||||
(s/def ::operations (s/coll-of ::operation))
|
||||
|
@ -472,16 +480,17 @@
|
|||
(s/keys :req-un [::id (or ::page-id ::component-id) ::operations]))
|
||||
|
||||
(defmethod change-spec :del-obj [_]
|
||||
(s/keys :req-un [::id ::page-id]))
|
||||
(s/keys :req-un [::id (or ::page-id ::component-id)]))
|
||||
|
||||
(s/def :internal.changes.reg-objects/shapes
|
||||
(s/coll-of uuid? :kind vector?))
|
||||
|
||||
(defmethod change-spec :reg-objects [_]
|
||||
(s/keys :req-un [::page-id :internal.changes.reg-objects/shapes]))
|
||||
(s/keys :req-un [(or ::page-id ::component-id)
|
||||
:internal.changes.reg-objects/shapes]))
|
||||
|
||||
(defmethod change-spec :mov-objects [_]
|
||||
(s/keys :req-un [::page-id ::parent-id :internal.shape/shapes]
|
||||
(s/keys :req-un [(or ::page-id ::component-id) ::parent-id :internal.shape/shapes]
|
||||
:opt-un [::index]))
|
||||
|
||||
(defmethod change-spec :add-page [_]
|
||||
|
@ -701,26 +710,26 @@
|
|||
(assoc data :options (d/dissoc-in (:options data) path)))))))
|
||||
|
||||
(defmethod process-change :add-obj
|
||||
[data {:keys [id obj page-id frame-id parent-id index] :as change}]
|
||||
(d/update-in-when data [:pages-index page-id]
|
||||
(fn [data]
|
||||
(let [parent-id (or parent-id frame-id)
|
||||
objects (:objects data)]
|
||||
(when (and (contains? objects parent-id)
|
||||
(contains? objects frame-id))
|
||||
(let [obj (assoc obj
|
||||
:frame-id frame-id
|
||||
:parent-id parent-id
|
||||
:id id)]
|
||||
(-> data
|
||||
(update :objects assoc id obj)
|
||||
(update-in [:objects parent-id :shapes]
|
||||
(fn [shapes]
|
||||
(let [shapes (or shapes [])]
|
||||
(cond
|
||||
(some #{id} shapes) shapes
|
||||
(nil? index) (conj shapes id)
|
||||
:else (cph/insert-at-index shapes index [id]))))))))))))
|
||||
[data {:keys [id obj page-id component-id frame-id parent-id index] :as change}]
|
||||
(let [update-fn (fn [data]
|
||||
(let [parent-id (or parent-id frame-id)
|
||||
objects (:objects data)]
|
||||
(let [obj (assoc obj
|
||||
:frame-id frame-id
|
||||
:parent-id parent-id
|
||||
:id id)]
|
||||
(-> data
|
||||
(update :objects assoc id obj)
|
||||
(update-in [:objects parent-id :shapes]
|
||||
(fn [shapes]
|
||||
(let [shapes (or shapes [])]
|
||||
(cond
|
||||
(some #{id} shapes) shapes
|
||||
(nil? index) (conj shapes id)
|
||||
:else (cph/insert-at-index shapes index [id])))))))))]
|
||||
(if page-id
|
||||
(d/update-in-when data [:pages-index page-id] update-fn)
|
||||
(d/update-in-when data [:components component-id] update-fn))))
|
||||
|
||||
(defmethod process-change :mod-obj
|
||||
[data {:keys [id page-id component-id operations] :as change}]
|
||||
|
@ -733,8 +742,8 @@
|
|||
(d/update-in-when data [:components component-id :objects] update-fn))))
|
||||
|
||||
(defmethod process-change :del-obj
|
||||
[data {:keys [page-id id] :as change}]
|
||||
(letfn [(delete-object [objects id]
|
||||
[data {:keys [page-id component-id id] :as change}]
|
||||
(letfn [(delete-object [objects]
|
||||
(if-let [target (get objects id)]
|
||||
(let [parent-id (cph/get-parent id objects)
|
||||
frame-id (:frame-id target)
|
||||
|
@ -752,7 +761,9 @@
|
|||
; dependend objects
|
||||
(as-> $ (reduce delete-object $ (:shapes target)))))
|
||||
objects))]
|
||||
(d/update-in-when data [:pages-index page-id :objects] delete-object id)))
|
||||
(if page-id
|
||||
(d/update-in-when data [:pages-index page-id :objects] delete-object)
|
||||
(d/update-in-when data [:components component-id :objects] delete-object))))
|
||||
|
||||
(defn rotation-modifiers
|
||||
[center shape angle]
|
||||
|
@ -765,7 +776,7 @@
|
|||
|
||||
;; reg-objects operation "regenerates" the values for the parent groups
|
||||
(defmethod process-change :reg-objects
|
||||
[data {:keys [page-id shapes]}]
|
||||
[data {:keys [page-id component-id shapes]}]
|
||||
(letfn [(reg-objects [objects]
|
||||
(reduce #(update %1 %2 update-group %1) objects
|
||||
(sequence (comp
|
||||
|
@ -797,10 +808,12 @@
|
|||
(assoc-in [:modifiers :rotation] (:rotation group 0))
|
||||
(geom/transform-shape))))]
|
||||
|
||||
(d/update-in-when data [:pages-index page-id :objects] reg-objects)))
|
||||
(if page-id
|
||||
(d/update-in-when data [:pages-index page-id :objects] reg-objects)
|
||||
(d/update-in-when data [:components component-id :objects] reg-objects))))
|
||||
|
||||
(defmethod process-change :mov-objects
|
||||
[data {:keys [parent-id shapes index page-id] :as change}]
|
||||
[data {:keys [parent-id shapes index page-id component-id] :as change}]
|
||||
(letfn [(is-valid-move? [objects shape-id]
|
||||
(let [invalid-targets (cph/calculate-invalid-targets shape-id objects)]
|
||||
(and (not (invalid-targets parent-id))
|
||||
|
@ -881,7 +894,9 @@
|
|||
(reduce (partial update-frame-ids frm-id) $ (get-in $ [parent-id :shapes])))
|
||||
objects)))]
|
||||
|
||||
(d/update-in-when data [:pages-index page-id :objects] move-objects)))
|
||||
(if page-id
|
||||
(d/update-in-when data [:pages-index page-id :objects] move-objects)
|
||||
(d/update-in-when data [:components component-id :objects] move-objects))))
|
||||
|
||||
(defmethod process-change :add-page
|
||||
[data {:keys [id name page]}]
|
||||
|
|
|
@ -42,11 +42,25 @@
|
|||
objects)
|
||||
nil)))
|
||||
|
||||
(defn make-container
|
||||
[page-or-component container-type]
|
||||
(assoc page-or-component
|
||||
:container-type container-type))
|
||||
|
||||
(defn is-page
|
||||
[container]
|
||||
(= (:container-type container) :page))
|
||||
|
||||
(defn is-component
|
||||
[container]
|
||||
(= (:container-type container) :component))
|
||||
|
||||
(defn get-container
|
||||
[page-id component-id local-file]
|
||||
(if (some? page-id)
|
||||
(get-in local-file [:pages-index page-id])
|
||||
(get-in local-file [:components component-id])))
|
||||
[container-id container-type local-file]
|
||||
(-> (if (= container-type :page)
|
||||
(get-in local-file [:pages-index container-id])
|
||||
(get-in local-file [:components container-id]))
|
||||
(assoc :container-type container-type)))
|
||||
|
||||
(defn get-shape
|
||||
[container shape-id]
|
||||
|
@ -59,6 +73,12 @@
|
|||
(get-in libraries [file-id :data]))]
|
||||
(get-in file [:components component-id])))
|
||||
|
||||
(defn is-master-of
|
||||
[shape-master shape-inst]
|
||||
(and (:shape-ref shape-inst)
|
||||
(or (= (:shape-ref shape-inst) (:id shape-master))
|
||||
(= (:shape-ref shape-inst) (:shape-ref shape-master)))))
|
||||
|
||||
(defn get-component-root
|
||||
[component]
|
||||
(get-in component [:objects (:id component)]))
|
||||
|
@ -75,12 +95,12 @@
|
|||
(defn get-children-objects
|
||||
"Retrieve all children objects recursively for a given object"
|
||||
[id objects]
|
||||
(map #(get objects %) (get-children id objects)))
|
||||
(mapv #(get objects %) (get-children id objects)))
|
||||
|
||||
(defn get-object-with-children
|
||||
"Retrieve a list with an object and all of its children"
|
||||
"Retrieve a vector with an object and all of its children"
|
||||
[id objects]
|
||||
(map #(get objects %) (cons id (get-children id objects))))
|
||||
(mapv #(get objects %) (cons id (get-children id objects))))
|
||||
|
||||
(defn is-shape-grouped
|
||||
"Checks if a shape is inside a group"
|
||||
|
@ -210,17 +230,17 @@
|
|||
:parent-id parent-id)
|
||||
|
||||
(some? (:shapes object))
|
||||
(assoc :shapes (map :id new-direct-children)))
|
||||
(assoc :shapes (mapv :id new-direct-children)))
|
||||
|
||||
new-object (update-new-object new-object object)
|
||||
|
||||
new-objects (concat [new-object] new-children)
|
||||
new-objects (d/concat [new-object] new-children)
|
||||
|
||||
updated-object (update-original-object object new-object)
|
||||
|
||||
updated-objects (if (identical? object updated-object)
|
||||
updated-children
|
||||
(concat [updated-object] updated-children))]
|
||||
(d/concat [updated-object] updated-children))]
|
||||
|
||||
[new-object new-objects updated-objects])
|
||||
|
||||
|
@ -232,9 +252,9 @@
|
|||
|
||||
(recur
|
||||
(next child-ids)
|
||||
(concat new-direct-children [new-child])
|
||||
(concat new-children new-child-objects)
|
||||
(concat updated-children updated-child-objects))))))))
|
||||
(d/concat new-direct-children [new-child])
|
||||
(d/concat new-children new-child-objects)
|
||||
(d/concat updated-children updated-child-objects))))))))
|
||||
|
||||
|
||||
(defn indexed-shapes
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
[cljs.spec.alpha :as s]
|
||||
[potok.core :as ptk]))
|
||||
|
||||
;; Change this to :info :debug or :trace to debug this module
|
||||
(log/set-level! :warn)
|
||||
|
||||
(declare sync-file)
|
||||
|
@ -493,16 +494,18 @@
|
|||
(ptk/reify ::reset-component
|
||||
ptk/WatchEvent
|
||||
(watch [_ state stream]
|
||||
;; ===== Uncomment this to debug =====
|
||||
(log/info :msg "RESET-COMPONENT of shape" :id (str id))
|
||||
(let [[rchanges uchanges]
|
||||
(dwlh/generate-sync-shape-and-children-components (get state :current-page-id)
|
||||
nil
|
||||
id
|
||||
(get state :workspace-data)
|
||||
(get state :workspace-libraries)
|
||||
true)]
|
||||
;; ===== Uncomment this to debug =====
|
||||
(let [local-file (get state :workspace-data)
|
||||
libraries (get state :workspace-libraries)
|
||||
container (cph/get-container (get state :current-page-id)
|
||||
:page
|
||||
local-file)
|
||||
[rchanges uchanges]
|
||||
(dwlh/generate-sync-shape-direct container
|
||||
id
|
||||
local-file
|
||||
libraries
|
||||
true)]
|
||||
(log/debug :msg "RESET-COMPONENT finished" :js/rchanges rchanges)
|
||||
|
||||
(rx/of (dwc/commit-changes rchanges uchanges {:commit-local? true}))))))
|
||||
|
@ -516,7 +519,6 @@
|
|||
(ptk/reify ::update-component
|
||||
ptk/WatchEvent
|
||||
(watch [_ state stream]
|
||||
;; ===== Uncomment this to debug =====
|
||||
(log/info :msg "UPDATE-COMPONENT of shape" :id (str id))
|
||||
(let [page-id (:current-page-id state)
|
||||
objects (dwc/lookup-page-objects state page-id)
|
||||
|
@ -529,7 +531,6 @@
|
|||
(get state :workspace-data)
|
||||
(get state :workspace-libraries))]
|
||||
|
||||
;; ===== Uncomment this to debug =====
|
||||
(log/debug :msg "UPDATE-COMPONENT finished" :js/rchanges rchanges)
|
||||
|
||||
(rx/of (dwc/commit-changes rchanges uchanges {:commit-local? true}))))))
|
||||
|
@ -552,7 +553,6 @@
|
|||
|
||||
ptk/WatchEvent
|
||||
(watch [_ state stream]
|
||||
;; ===== Uncomment this to debug =====
|
||||
(log/info :msg "SYNC-FILE" :file (str (or file-id "local")))
|
||||
(let [library-changes [(dwlh/generate-sync-library :components file-id state)
|
||||
(dwlh/generate-sync-library :colors file-id state)
|
||||
|
@ -566,7 +566,6 @@
|
|||
uchanges (d/concat []
|
||||
(->> library-changes (remove nil?) (map second) (flatten))
|
||||
(->> file-changes (remove nil?) (map second) (flatten)))]
|
||||
;; ===== Uncomment this to debug =====
|
||||
(log/debug :msg "SYNC-FILE finished" :js/rchanges rchanges)
|
||||
(rx/concat
|
||||
(rx/of (dm/hide-tag :sync-dialog))
|
||||
|
@ -593,14 +592,12 @@
|
|||
(ptk/reify ::sync-file-2nd-stage
|
||||
ptk/WatchEvent
|
||||
(watch [_ state stream]
|
||||
;; ===== Uncomment this to debug =====
|
||||
(log/info :msg "SYNC-FILE (2nd stage)" :file (str (or file-id "local")))
|
||||
(let [[rchanges1 uchanges1] (dwlh/generate-sync-file :components nil state)
|
||||
[rchanges2 uchanges2] (dwlh/generate-sync-library :components file-id state)
|
||||
rchanges (d/concat rchanges1 rchanges2)
|
||||
uchanges (d/concat uchanges1 uchanges2)]
|
||||
(when rchanges
|
||||
;; ===== Uncomment this to debug =====
|
||||
(log/debug :msg "SYNC-FILE (2nd stage) finished" :js/rchanges rchanges)
|
||||
(rx/of (dwc/commit-changes rchanges uchanges {:commit-local? true})))))))
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue