🐛 Fix ungroup component

This commit is contained in:
Pablo Alba 2023-06-15 14:25:09 +02:00 committed by Andrés Moya
parent 128fe29619
commit 7733bc4419
3 changed files with 167 additions and 101 deletions

View file

@ -328,6 +328,9 @@
The list of objects are returned in tree traversal order, respecting
the order of the children of each parent."
([object parent-id objects]
(clone-object object parent-id objects (fn [object _] object) (fn [object _] object) nil false))
([object parent-id objects update-new-object]
(clone-object object parent-id objects update-new-object (fn [object _] object) nil false))

View file

@ -12,10 +12,14 @@
[app.common.pages.changes-builder :as pcb]
[app.common.pages.helpers :as cph]
[app.common.types.component :as ctk]
[app.common.types.pages-list :as ctpl]
[app.common.types.shape :as cts]
[app.common.types.shape-tree :as ctst]
[app.common.types.shape.layout :as ctl]
[app.common.uuid :as uuid]
[app.main.data.workspace.changes :as dch]
[app.main.data.workspace.selection :as dws]
[app.main.data.workspace.shapes :as dwsh]
[app.main.data.workspace.state-helpers :as wsh]
[app.main.data.workspace.undo :as dwu]
[beicon.core :as rx]
@ -150,6 +154,46 @@
(pcb/change-parent parent-id children idx-in-parent)
(pcb/remove-objects [(:id frame)]))))
(defn- clone-component-shapes-changes
[changes shape objects]
(let [shape-parent-id (:parent-id shape)
new-shape-id (uuid/next)
[_ new-shapes _]
(ctst/clone-object shape
shape-parent-id
objects
(fn [object _]
(cond-> object
(= new-shape-id (:parent-id object))
(assoc :parent-id shape-parent-id)))
(fn [object _] object)
new-shape-id
false)
new-shapes (->> new-shapes
(filter #(not= (:id %) new-shape-id)))]
(reduce
(fn [changes shape]
(pcb/add-object changes shape))
changes
new-shapes)))
(defn remove-component-changes
[it page-id shape objects file-data file]
(let [page (ctpl/get-page file-data page-id)
components-v2 (dm/get-in file-data [:options :components-v2])
;; In order to ungroup a component, we first make a clone of its shapes,
;; and then we delete it
changes (-> (pcb/empty-changes it page-id)
(pcb/with-objects objects)
(pcb/with-library-data file-data)
(pcb/with-page page)
(clone-component-shapes-changes shape objects)
(dwsh/delete-shapes-changes file page objects [(:id shape)] it components-v2))]
;; TODO: Should we call detach-comment-thread ?
changes))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; GROUPS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -175,11 +219,16 @@
(watch [it state _]
(let [page-id (:current-page-id state)
objects (wsh/lookup-page-objects state page-id)
file-data (get state :workspace-data)
file (wsh/get-local-file state)
prepare
(fn [shape-id]
(let [shape (get objects shape-id)]
(cond
(ctk/main-instance? shape)
(remove-component-changes it page-id shape objects file-data file)
(or (cph/group-shape? shape) (cph/bool-shape? shape))
(remove-group-changes it page-id shape objects)

View file

@ -214,10 +214,15 @@
(real-delete-shapes file page objects ids-to-delete it components-v2)
(rx/of (dwu/commit-undo-transaction undo-id))))))))
(defn- real-delete-shapes
[file page objects ids it components-v2]
(defn- real-delete-shapes-changes
([file page objects ids it components-v2]
(let [changes (-> (pcb/empty-changes it (:id page))
(pcb/with-page page)
(pcb/with-objects objects)
(pcb/with-library-data file))]
(real-delete-shapes-changes changes file page objects ids it components-v2)))
([changes file page objects ids _it components-v2]
(let [lookup (d/getf objects)
groups-to-unmask
(reduce (fn [group-ids id]
;; When the shape to delete is the mask of a masked group,
@ -300,10 +305,7 @@
(into ids all-children))
[])
changes (-> (pcb/empty-changes it (:id page))
(pcb/with-page page)
(pcb/with-objects objects)
(pcb/with-library-data file)
changes (-> changes
(pcb/set-page-option :guides guides))
changes (reduce (fn [changes component-id]
@ -332,7 +334,19 @@
(cond-> (seq starting-flows)
(pcb/update-page-option :flows (fn [flows]
(->> (map :id starting-flows)
(reduce ctp/remove-flow flows))))))
(reduce ctp/remove-flow flows))))))]
[changes all-parents])))
(defn delete-shapes-changes
[changes file page objects ids it components-v2]
(let [[changes _all-parents] (real-delete-shapes-changes changes file page objects ids it components-v2)]
changes))
(defn- real-delete-shapes
[file page objects ids it components-v2]
(let [[changes all-parents] (real-delete-shapes-changes file page objects ids it components-v2)
undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dc/detach-comment-thread ids)