From 9fb0cbff4670acda700f4dfa73e99267477bca48 Mon Sep 17 00:00:00 2001 From: Konstantinos Kaloutas Date: Sat, 10 Feb 2024 13:07:57 +0200 Subject: [PATCH] select overlapping page on create --- src/renderer/element/handlers.cljs | 31 +++++++++++++++++------------- src/renderer/tools/element.cljs | 1 + 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/renderer/element/handlers.cljs b/src/renderer/element/handlers.cljs index 1e34ed19..3ea69f38 100644 --- a/src/renderer/element/handlers.cljs +++ b/src/renderer/element/handlers.cljs @@ -429,30 +429,35 @@ :selected? true :children []}) +(defn overlapping-page + [db el] + (or + (some #(when (bounds/contained? (tools/bounds %) (tools/bounds el)) %) (pages db)) + (some #(when (bounds/intersected? (tools/bounds %) (tools/bounds el)) %) (pages db)) + (active-page db))) + (defn create [db el] (let [key (uuid/generate) + page (overlapping-page db el) parent (or (:parent el) - (if (page? el) :canvas (-> db active-page :key))) - el (map/deep-merge el default-props {:key key :parent parent})] + (if (page? el) :canvas (:key page))) + el (map/deep-merge el default-props {:key key :parent parent}) + [x1 y1 _ _] (tools/bounds page)] (cond-> db :always (-> (assoc-in (conj (path db) key) el) - (update-prop (:parent el) :children #(vec (conj % key)))) + (update-prop (:parent el) :children #(vec (conj % key))) + (set-active-page (if (page? el) key (:key page)))) - (not= (:tool db) :select) - (tools/set-tool :select) - - (page? el) - (set-active-page key)))) + (not (page? el)) + (translate [(- x1) (- y1)])))) (defn add ([db] - (let [[x1 y1 _ _] (tools/bounds (active-page db))] - (cond-> db - :always (add (get-temp db)) - (not (page? (get-temp db))) (translate [(- x1) (- y1)]) - :always clear-temp))) + (-> db + (add (get-temp db)) + clear-temp)) ([db & elements] (reduce create (deselect db) elements))) ; TODO: Handle children diff --git a/src/renderer/tools/element.cljs b/src/renderer/tools/element.cljs index ff004b8f..4da01957 100644 --- a/src/renderer/tools/element.cljs +++ b/src/renderer/tools/element.cljs @@ -28,6 +28,7 @@ (let [temp-element (get-in db [:documents (:active-document db) :temp-element])] (-> db element.h/add + (tools/set-tool :select) (history.h/finalize "Create " (name (:tag temp-element))) (assoc :cursor "crosshair"))))