diff --git a/common/uxbox/common/pages.cljc b/common/uxbox/common/pages.cljc index cd0d0d8a4..10e8f730a 100644 --- a/common/uxbox/common/pages.cljc +++ b/common/uxbox/common/pages.cljc @@ -139,7 +139,7 @@ (defmethod change-spec-impl :add-obj [_] (s/keys :req-un [::id ::frame-id ::obj] - :opt-un [::session-id])) + :opt-un [::session-id ::parent-id])) (defmethod change-spec-impl :mod-obj [_] (s/keys :req-un [::id ::operations] @@ -247,7 +247,7 @@ (contains? (:objects data) frame-id) (update-in [:objects frame-id :shapes] (fn [s] (filterv #(not= % id) s))) - (seq shapes) ; Recursive delete all dependend objects + (seq shapes) ; Recursive delete all dependend objects (as-> $ (reduce #(or (process-change %1 {:type :del-obj :id %2}) %1) $ shapes)))))) (defn- calculate-child-parent-map diff --git a/frontend/src/uxbox/main/data/helpers.cljs b/frontend/src/uxbox/main/data/helpers.cljs index b0ab21386..8fc13ef4d 100644 --- a/frontend/src/uxbox/main/data/helpers.cljs +++ b/frontend/src/uxbox/main/data/helpers.cljs @@ -7,16 +7,15 @@ ;; ;; Copyright (c) 2020 UXBOX Labs SL -(ns uxbox.main.data.helpers) +(ns uxbox.main.data.helpers + (:require [uxbox.common.data :as d])) (defn get-children "Retrieve all children ids recursively for a given shape" [shape-id objects] (let [shapes (get-in objects [shape-id :shapes])] (if shapes - (concat - shapes - (mapcat #(get-children % objects) shapes)) + (d/concat shapes (mapcat #(get-children % objects) shapes)) []))) (defn is-shape-grouped @@ -25,11 +24,11 @@ (let [contains-shape-fn (fn [{:keys [shapes]}] ((set shapes) shape-id)) - shapes (remove #(= (:type %) :frame) (vals objects))] + shapes (remove #(= (:type %) :frame) (vals objects))] (some contains-shape-fn shapes))) (defn get-parent - "Retrieve the id of the parent for the shape-id (if exists" + "Retrieve the id of the parent for the shape-id (if exists)" [shape-id objects] (let [check-parenthood (fn [shape] (when (and (:shapes shape) @@ -55,9 +54,10 @@ (rec-fn shape-id []))) (defn replace-shapes - "Replace inside shapes the value `to-replace-id` for the value in items keeping the same order. - `to-replace-id` can be a set, a sequable or a single value. Any of these will be changed into a - set to make the replacement" + "Replace inside shapes the value `to-replace-id` for the value in + items keeping the same order. `to-replace-id` can be a set, a + sequable or a single value. Any of these will be changed into a set + to make the replacement" [shape to-replace-id items] (let [should-replace (cond diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index ea1456909..db6807c23 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -1318,17 +1318,28 @@ (let [page-id (::page-id state) session-id (:session-id state) objects (get-in state [:workspace-data page-id :objects]) - rchanges (mapv #(array-map :type :del-obj :id %) ids) - uchanges (mapv (fn [id] - (let [obj (get objects id) - frm (get objects (:frame-id obj)) - idx (d/index-of (:shapes frm) id)] - {:type :add-obj - :id id - :frame-id (:id frm) - :index idx - :obj obj})) - (reverse ids))] + cpindex (helpers/calculate-child-parent-map objects) + + del-change #(array-map :type :del-obj :id %) + + rchanges + (reduce (fn [res id] + (let [chd (helpers/get-children id objects)] + (into res (d/concat + (mapv del-change (reverse chd)) + [(del-change id)])))) + [] + ids) + + uchanges + (mapv (fn [id] + (let [obj (get objects id)] + {:type :add-obj + :id id + :frame-id (:frame-id obj) + :parent-id (get cpindex id) + :obj obj})) + (reverse (map :id rchanges)))] (rx/of (commit-changes rchanges uchanges {:commit-local? true})))))) (defn- delete-frame