From 4fc3f316e02362052dcca4df3bdbaff54c504eb6 Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Tue, 27 Dec 2022 07:03:12 +0100 Subject: [PATCH] :bug: Add function to reparent orphan shapes --- frontend/src/app/main/data/workspace.cljs | 21 +++++++++++++++++-- .../main/data/workspace/state_helpers.cljs | 15 +++++++++++-- frontend/src/debug.cljs | 3 +++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/frontend/src/app/main/data/workspace.cljs b/frontend/src/app/main/data/workspace.cljs index 206deef13..6ce0e0e0b 100644 --- a/frontend/src/app/main/data/workspace.cljs +++ b/frontend/src/app/main/data/workspace.cljs @@ -696,7 +696,7 @@ (pcb/resize-parents parents)))) (defn relocate-shapes - [ids parent-id to-index] + [ids parent-id to-index & [ignore-parents?]] (us/verify (s/coll-of ::us/uuid) ids) (us/verify ::us/uuid parent-id) (us/verify number? to-index) @@ -712,7 +712,9 @@ ;; If we try to move a parent into a child we remove it ids (filter #(not (cph/is-parent? objects parent-id %)) ids) - parents (into #{parent-id} (map #(cph/get-parent-id objects %)) ids) + parents (if ignore-parents? + #{parent-id} + (into #{parent-id} (map #(cph/get-parent-id objects %)) ids)) groups-to-delete (loop [current-id (first parents) @@ -1832,6 +1834,21 @@ (rx/empty))))) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Orphan Shapes +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + + +(defn fix-orphan-shapes + [] + (ptk/reify ::fix-orphan-shapes + ptk/WatchEvent + (watch [_ state _] + (let [orphans (set (into [] (keys (wsh/find-orphan-shapes state))))] + (rx/of (relocate-shapes orphans uuid/zero 0 true)))))) + + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Inspect ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff --git a/frontend/src/app/main/data/workspace/state_helpers.cljs b/frontend/src/app/main/data/workspace/state_helpers.cljs index b6596511b..dd01688d6 100644 --- a/frontend/src/app/main/data/workspace/state_helpers.cljs +++ b/frontend/src/app/main/data/workspace/state_helpers.cljs @@ -11,7 +11,8 @@ [app.common.geom.point :as gpt] [app.common.geom.shapes :as gsh] [app.common.pages.helpers :as cph] - [app.common.path.commands :as upc])) + [app.common.path.commands :as upc] + [app.common.uuid :as uuid])) (defn lookup-page ([state] @@ -146,4 +147,14 @@ (let [{:keys [x y width height]} (get-in state [:workspace-local :vbox])] (gpt/point (+ x (/ width 2)) (+ y (/ height 2))))) - +(defn find-orphan-shapes + ([state] + (find-orphan-shapes state (:current-page-id state))) + ([state page-id] + (let [objects (lookup-page-objects state page-id) + objects (filter (fn [item] + (and + (not= (key item) uuid/zero) + (not (contains? objects (:parent-id (val item)))))) + objects)] + objects))) diff --git a/frontend/src/debug.cljs b/frontend/src/debug.cljs index cfb8b1c40..f2d2e729a 100644 --- a/frontend/src/debug.cljs +++ b/frontend/src/debug.cljs @@ -346,3 +346,6 @@ [read-only?] (st/emit! (dw/set-workspace-read-only read-only?))) +(defn ^:export fix-orphan-shapes + [] + (st/emit! (dw/fix-orphan-shapes)))