From 1dfc604cf040d1e28cc16fddf3676c3a0282c3bb Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 15 Jun 2020 14:02:58 +0200 Subject: [PATCH] :bug: Fix vertical positioning. --- .../tests/uxbox/tests/test_common_pages.clj | 109 ------------------ common/uxbox/common/pages.cljc | 36 +----- frontend/src/uxbox/main/data/workspace.cljs | 40 ++++--- .../src/uxbox/main/data/workspace/common.cljs | 25 ++-- 4 files changed, 39 insertions(+), 171 deletions(-) diff --git a/backend/tests/uxbox/tests/test_common_pages.clj b/backend/tests/uxbox/tests/test_common_pages.clj index e30eaa52f..78d994dbf 100644 --- a/backend/tests/uxbox/tests/test_common_pages.clj +++ b/backend/tests/uxbox/tests/test_common_pages.clj @@ -165,115 +165,6 @@ res (cp/process-changes data [chg])] (t/is (= 0 (count (:objects res)))))) -(t/deftest process-change-mod-obj-abs-order - (let [id1 (uuid/next) - id2 (uuid/next) - id3 (uuid/next) - data (-> cp/default-page-data - (assoc-in [:objects uuid/zero :shapes] [id1 id2 id3]))] - - (t/testing "abs order 1" - (let [chg {:type :mod-obj - :id uuid/zero - :operations [{:type :abs-order - :id id3 - :index 0}]} - res (cp/process-changes data [chg])] - - ;; (clojure.pprint/pprint data) - ;; (clojure.pprint/pprint res) - - (t/is (= [id3 id1 id2] (get-in res [:objects uuid/zero :shapes]))))) - - (t/testing "abs order 2" - (let [chg {:type :mod-obj - :id uuid/zero - :operations [{:type :abs-order - :id id1 - :index 100}]} - res (cp/process-changes data [chg])] - - ;; (clojure.pprint/pprint data) - ;; (clojure.pprint/pprint res) - - (t/is (= [id2 id3 id1] (get-in res [:objects uuid/zero :shapes]))))) - - (t/testing "abs order 3" - (let [chg {:type :mod-obj - :id uuid/zero - :operations [{:type :abs-order - :id id3 - :index 1}]} - res (cp/process-changes data [chg])] - - ;; (clojure.pprint/pprint data) - ;; (clojure.pprint/pprint res) - - (t/is (= [id1 id3 id2] (get-in res [:objects uuid/zero :shapes]))))) - )) - - -(t/deftest process-change-mod-obj-rel-order - (let [id1 (uuid/next) - id2 (uuid/next) - id3 (uuid/next) - data (-> cp/default-page-data - (assoc-in [:objects uuid/zero :shapes] [id1 id2 id3]))] - - (t/testing "rel order 1" - (let [chg {:type :mod-obj - :id uuid/zero - :operations [{:type :rel-order - :id id3 - :loc :down}]} - res (cp/process-changes data [chg])] - - ;; (clojure.pprint/pprint data) - ;; (clojure.pprint/pprint res) - - (t/is (= [id1 id3 id2] (get-in res [:objects uuid/zero :shapes]))))) - - - (t/testing "rel order 2" - (let [chg {:type :mod-obj - :id uuid/zero - :operations [{:type :rel-order - :id id1 - :loc :top}]} - res (cp/process-changes data [chg])] - - ;; (clojure.pprint/pprint data) - ;; (clojure.pprint/pprint res) - - (t/is (= [id2 id3 id1] (get-in res [:objects uuid/zero :shapes]))))) - - (t/testing "rel order 3" - (let [chg {:type :mod-obj - :id uuid/zero - :operations [{:type :rel-order - :id id2 - :loc :up}]} - res (cp/process-changes data [chg])] - - ;; (clojure.pprint/pprint data) - ;; (clojure.pprint/pprint res) - - (t/is (= [id1 id3 id2] (get-in res [:objects uuid/zero :shapes]))))) - - (t/testing "rel order 4" - (let [chg {:type :mod-obj - :id uuid/zero - :operations [{:type :rel-order - :id id3 - :loc :bottom}]} - res (cp/process-changes data [chg])] - - ;; (clojure.pprint/pprint data) - ;; (clojure.pprint/pprint res) - - (t/is (= [id3 id1 id2] (get-in res [:objects uuid/zero :shapes]))))) - )) - (t/deftest process-change-move-objects (let [frame-a-id (uuid/custom 1) frame-b-id (uuid/custom 2) diff --git a/common/uxbox/common/pages.cljc b/common/uxbox/common/pages.cljc index 07e05a907..c018d243f 100644 --- a/common/uxbox/common/pages.cljc +++ b/common/uxbox/common/pages.cljc @@ -156,21 +156,14 @@ (s/def ::ids (s/coll-of ::us/uuid)) (s/def ::attr keyword?) -(s/def ::val any?) +(s/def ::val any?) (s/def ::frame-id uuid?) -(s/def ::loc #{:top :bottom :up :down}) (defmulti operation-spec-impl :type) (defmethod operation-spec-impl :set [_] (s/keys :req-un [::attr ::val])) -(defmethod operation-spec-impl :abs-order [_] - (s/keys :req-un [::id ::index])) - -(defmethod operation-spec-impl :rel-order [_] - (s/keys :req-un [::id ::loc])) - (s/def ::operation (s/multi-spec operation-spec-impl :type)) (s/def ::operations (s/coll-of ::operation)) @@ -465,7 +458,6 @@ (reduce remove-from-old-parent $ shapes) (reduce update-frame-ids $ (get-in $ [:objects parent-id :shapes])))))) - (defmethod process-operation :set [shape op] (let [attr (:attr op) @@ -474,32 +466,6 @@ (dissoc shape attr) (assoc shape attr val)))) -(defmethod process-operation :abs-order - [obj {:keys [id index]}] - (assert (vector? (:shapes obj)) ":shapes should be a vector") - (update obj :shapes (fn [items] - (let [[b a] (->> (remove #(= % id) items) - (split-at index))] - (vec (concat b [id] a)))))) - - -(defmethod process-operation :rel-order - [obj {:keys [id loc] :as change}] - (assert (vector? (:shapes obj)) ":shapes should be a vector") - (let [shapes (:shapes obj) - cindex (d/index-of shapes id) - nindex (case loc - :top (- (count shapes) 1) - :down (max 0 (- cindex 1)) - :up (min (- (count shapes) 1) (inc cindex)) - :bottom 0)] - (update obj :shapes - (fn [shapes] - (let [[fst snd] (->> (remove #(= % id) shapes) - (split-at nindex))] - (d/concat [] fst [id] snd)))))) - - (defmethod process-operation :default [shape op] (ex/raise :type :operation-not-implemented diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index baf5f644b..c1bc180b9 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -748,23 +748,33 @@ (ptk/reify ::vertical-order-selected-shpes ptk/WatchEvent (watch [_ state stream] - (let [page-id (:current-page-id state) - objects (get-in state [:workspace-data page-id :objects]) - selected (seq (get-in state [:workspace-local :selected])) - + (let [page-id (:current-page-id state) + objects (get-in state [:workspace-data page-id :objects]) + selected (get-in state [:workspace-local :selected]) rchanges (mapv (fn [id] - (let [frame-id (get-in objects [id :frame-id])] - {:type :mod-obj - :id frame-id - :operations [{:type :rel-order :id id :loc loc}]})) + (let [obj (get objects id) + parent (get objects (:parent-id obj)) + shapes (:shapes parent) + cindex (d/index-of shapes id) + nindex (case loc + :top (count shapes) + :down (max 0 (- cindex 1)) + :up (min (count shapes) (+ (inc cindex) 1)) + :bottom 0)] + {:type :mov-objects + :parent-id (:parent-id obj) + :frame-id (:frame-id obj) + :index nindex + :shapes [id]})) selected) - uchanges (mapv (fn [id] - (let [frame-id (get-in objects [id :frame-id]) - shapes (get-in objects [frame-id :shapes]) - cindex (d/index-of shapes id)] - {:type :mod-obj - :id frame-id - :operations [{:type :abs-order :id id :index cindex}]})) + + uchanges (mapv (fn [id] + (let [obj (get objects id)] + {:type :mov-objects + :parent-id (:parent-id obj) + :frame-id (:frame-id obj) + :shapes [id] + :index (cph/position-on-parent id objects)})) selected)] (rx/of (dwc/commit-changes rchanges uchanges {:commit-local? true})))))) diff --git a/frontend/src/uxbox/main/data/workspace/common.cljs b/frontend/src/uxbox/main/data/workspace/common.cljs index 5f97ce2b0..ffd136b99 100644 --- a/frontend/src/uxbox/main/data/workspace/common.cljs +++ b/frontend/src/uxbox/main/data/workspace/common.cljs @@ -76,24 +76,25 @@ [ma mb] (let [ma-keys (set (keys ma)) mb-keys (set (keys mb)) - added (set/difference mb-keys ma-keys) + added (set/difference mb-keys ma-keys) removed (set/difference ma-keys mb-keys) - both (set/intersection ma-keys mb-keys)] + both (set/intersection ma-keys mb-keys)] (d/concat (mapv #(array-map :type :set :attr % :val (get mb %)) added) (mapv #(array-map :type :set :attr % :val nil) removed) - (loop [k (first both) - r (rest both) - rs []] - (if k - (let [vma (get ma k) + (loop [items (seq both) + result []] + (if items + (let [k (first items) + vma (get ma k) vmb (get mb k)] (if (= vma vmb) - (recur (first r) (rest r) rs) - (recur (first r) (rest r) (conj rs {:type :set - :attr k - :val vmb})))) - rs))))) + (recur (next items) result) + (recur (next items) + (conj result {:type :set + :attr k + :val vmb})))) + result))))) (defn- generate-changes [prev curr]