From 25518a4ac011aef4e8f09da6078e2fb5dfcb7ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Thu, 21 May 2020 10:56:42 +0200 Subject: [PATCH] :bug: Implement undo in layer move operation --- common/uxbox/common/pages.cljc | 11 +++++++++-- frontend/src/uxbox/main/data/workspace.cljs | 16 ++++++++++------ .../uxbox/main/ui/workspace/sidebar/layers.cljs | 4 ++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/common/uxbox/common/pages.cljc b/common/uxbox/common/pages.cljc index 673c8d6be..f4c04d406 100644 --- a/common/uxbox/common/pages.cljc +++ b/common/uxbox/common/pages.cljc @@ -72,6 +72,12 @@ (into result (calculate-invalid-targets child-id objects)))] (reduce reduce-fn result children))) +(defn- valid-frame-target + [shape-id parent-id objects] + (let [shape (get objects shape-id)] + (or (not= (:type shape) :frame) + (= parent-id uuid/zero)))) + (defn- insert-at-index [shapes index ids] (let [[before after] (split-at index shapes) @@ -389,8 +395,9 @@ ;; Check if the move from shape-id -> parent-id is valid is-valid-move (fn [shape-id] - (let [invalid (calculate-invalid-targets shape-id (:objects data))] - (not (invalid parent-id)))) + (let [invalid-targets (calculate-invalid-targets shape-id (:objects data))] + (and (not (invalid-targets parent-id)) + (valid-frame-target shape-id parent-id (:objects data))))) valid? (every? is-valid-move shapes) diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index 32cf0b413..a72937ebe 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -916,13 +916,11 @@ ;; --- Change Shape Order (D&D Ordering) -;; TODO: pending UNDO - (defn relocate-shape - [id parent-id index] + [id parent-id to-index] (us/verify ::us/uuid id) (us/verify ::us/uuid parent-id) - (us/verify number? index) + (us/verify number? to-index) (ptk/reify ::relocate-shape dwc/IUpdateGroup @@ -931,12 +929,18 @@ ptk/WatchEvent (watch [_ state stream] (let [page-id (:current-page-id state) + objects (get-in state [:workspace-data page-id :objects]) + parent (get objects (cp/get-parent id objects)) + current-index (d/index-of (:shapes parent) id) selected (get-in state [:workspace-local :selected])] (rx/of (dwc/commit-changes [{:type :mov-objects :parent-id parent-id - :index index + :index to-index + :shapes (vec selected)}] + [{:type :mov-objects + :parent-id (:id parent) + :index current-index :shapes (vec selected)}] - [] {:commit-local? true})))))) ;; --- Change Page Order (D&D Ordering) diff --git a/frontend/src/uxbox/main/ui/workspace/sidebar/layers.cljs b/frontend/src/uxbox/main/ui/workspace/sidebar/layers.cljs index af72e287a..406c7f1c8 100644 --- a/frontend/src/uxbox/main/ui/workspace/sidebar/layers.cljs +++ b/frontend/src/uxbox/main/ui/workspace/sidebar/layers.cljs @@ -153,9 +153,9 @@ (fn [side {:keys [id] :as data}] (if (= side :center) (st/emit! (dw/relocate-shape id (:id item) 0)) - (let [index (if (= side :top) (inc index) index) + (let [to-index (if (= side :top) (inc index) index) parent-id (cp/get-parent (:id item) objects)] - (st/emit! (dw/relocate-shape id parent-id index))))) + (st/emit! (dw/relocate-shape id parent-id to-index))))) [dprops dref] (hooks/use-sortable :type (str (:frame-id item))