From 16a4d4c8b443e9800976326440834123fc3bbdfd Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 23 Jan 2016 22:08:15 +0200 Subject: [PATCH] Improved delete-shape event. --- src/uxbox/data/workspace.cljs | 44 ++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/uxbox/data/workspace.cljs b/src/uxbox/data/workspace.cljs index c149026e88..1e842f5aaa 100644 --- a/src/uxbox/data/workspace.cljs +++ b/src/uxbox/data/workspace.cljs @@ -138,26 +138,42 @@ (assoc-in $ [:shapes-by-id sid] shape)))))) -;; FIXME: delete shape that is part of group. (defn delete-shape "Remove the shape using its id." [id] - (letfn [(dissoc-shape [state {:keys [id] :as shape}] - (if (= (:type shape) :builtin/group) - (let [state (update-in state [:shapes-by-id] dissoc id)] - (->> (map #(get-in state [:shapes-by-id %]) (:items shape)) - (reduce dissoc-shape state))) - (update-in state [:shapes-by-id] dissoc id)))] + (letfn [(dissoc-group [state {:keys [id] :as shape}] + (let [state (update-in state [:shapes-by-id] dissoc id)] + (->> (:items shape) + (map #(get-in state [:shapes-by-id %])) + (reduce dissoc-from-index state)))) + + (dissoc-icon [state {:keys [id] :as shape}] + (update-in state [:shapes-by-id] dissoc id)) + + (dissoc-from-group [state {:keys [id group] :as shape}] + (if-let [group' (get-in state [:shapes-by-id group])] + (as-> (:items group') $ + (into [] (remove #(= % id) $)) + (assoc-in state [:shapes-by-id group :items] $)) + state)) + + (dissoc-from-page [state {:keys [page id] :as shape}] + (as-> (get-in state [:pages-by-id page :shapes]) $ + (into [] (remove #(= % id) $)) + (assoc-in state [:pages-by-id page :shapes] $))) + + (dissoc-from-index [state shape] + (case (:type shape) + :builtin/icon (dissoc-icon state shape) + :builtin/group (dissoc-group state shape)))] (reify rs/UpdateEvent (-apply-update [_ state] - (let [shape (get-in state [:shapes-by-id id]) - pageid (:page shape) - shapes (get-in state [:pages-by-id pageid :shapes]) - shapes (into [] (remove #(= % id) shapes))] - (as-> state $ - (assoc-in $ [:pages-by-id pageid :shapes] shapes) - (dissoc-shape $ shape))))))) + (let [shape (get-in state [:shapes-by-id id])] + (as-> state $ + (dissoc-from-page $ shape) + (dissoc-from-group $ shape) + (dissoc-from-index $ shape))))))) (defn move-shape "Mark a shape selected for drawing in the canvas."