From 6e0433a34bfd90052c1573c8930c17f8eaaeaa74 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 25 Jan 2022 14:48:54 +0100 Subject: [PATCH] :sparkles: Review changes --- .../src/app/common/pages/changes_builder.cljc | 38 ++++++++++--- .../src/app/main/data/workspace/guides.cljs | 55 +++++++------------ .../main/data/workspace/state_helpers.cljs | 6 ++ .../ui/workspace/viewport/snap_points.cljs | 5 +- frontend/src/app/util/snap_data.cljs | 48 +++++++++------- frontend/src/app/worker/snaps.cljs | 9 +-- 6 files changed, 91 insertions(+), 70 deletions(-) diff --git a/common/src/app/common/pages/changes_builder.cljc b/common/src/app/common/pages/changes_builder.cljc index d39374224..81e913975 100644 --- a/common/src/app/common/pages/changes_builder.cljc +++ b/common/src/app/common/pages/changes_builder.cljc @@ -13,12 +13,21 @@ ;; Auxiliary functions to help create a set of changes (undo + redo) (defn empty-changes - [origin page-id] - (let [changes {:redo-changes [] - :undo-changes [] - :origin origin}] - (with-meta changes - {::page-id page-id}))) + ([origin page-id] + (let [changes (empty-changes origin)] + (with-meta changes + {::page-id page-id}))) + + ([origin] + {:redo-changes [] + :undo-changes [] + :origin origin})) + +(defn with-page [changes page] + (vary-meta changes assoc + ::page page + ::page-id (:id page) + ::objects (:objects page))) (defn with-objects [changes objects] (vary-meta changes assoc ::objects objects)) @@ -167,10 +176,25 @@ (reduce add-undo-change-parent $ ids) (reduce add-undo-change-shape $ ids)))))) - (defn move-page [chdata index prev-index] (let [page-id (::page-id (meta chdata))] (-> chdata (update :redo-changes conj {:type :mov-page :id page-id :index index}) (update :undo-changes conj {:type :mov-page :id page-id :index prev-index})))) + +(defn set-page-option + [chdata option-key option-val] + (let [page-id (::page-id (meta chdata)) + page (::page (meta chdata)) + old-val (get-in page [:options option-key])] + + (-> chdata + (update :redo-changes conj {:type :set-option + :page-id page-id + :option option-key + :value option-val}) + (update :undo-changes conj {:type :set-option + :page-id page-id + :option option-key + :value old-val})))) diff --git a/frontend/src/app/main/data/workspace/guides.cljs b/frontend/src/app/main/data/workspace/guides.cljs index ba614eebd..c446d8876 100644 --- a/frontend/src/app/main/data/workspace/guides.cljs +++ b/frontend/src/app/main/data/workspace/guides.cljs @@ -8,6 +8,7 @@ (:require [app.common.geom.point :as gpt] [app.common.geom.shapes :as gsh] + [app.common.pages.changes-builder :as pcb] [app.common.spec :as us] [app.common.types.page-options :as tpo] [app.main.data.workspace.changes :as dwc] @@ -27,47 +28,30 @@ (ptk/reify ::update-guides ptk/WatchEvent (watch [it state _] - (let [page-id (:current-page-id state) - guides (-> state wsh/lookup-page-options (:guides {})) - + (let [page (wsh/lookup-page state) + guides (get-in page [:options :guides] {}) new-guides (assoc guides (:id guide) guide) - - rch [{:type :set-option - :page-id page-id - :option :guides - :value new-guides}] - uch [{:type :set-option - :page-id page-id - :option :guides - :value guides}]] - (rx/of - (dwc/commit-changes - {:redo-changes rch - :undo-changes uch - :origin it})))))) + + changes + (-> (pcb/empty-changes it) + (pcb/with-page page) + (pcb/set-page-option :guides new-guides))] + (rx/of (dwc/commit-changes changes)))))) (defn remove-guide [guide] (us/verify ::tpo/guide guide) (ptk/reify ::remove-guide ptk/WatchEvent (watch [it state _] - (let [page-id (:current-page-id state) - guides (-> state wsh/lookup-page-options (:guides {})) + (let [page (wsh/lookup-page state) + guides (get-in page [:options :guides] {}) new-guides (dissoc guides (:id guide)) - - rch [{:type :set-option - :page-id page-id - :option :guides - :value new-guides}] - uch [{:type :set-option - :page-id page-id - :option :guides - :value guides}]] - (rx/of - (dwc/commit-changes - {:redo-changes rch - :undo-changes uch - :origin it})))))) + + changes + (-> (pcb/empty-changes it) + (pcb/with-page page) + (pcb/set-page-option :guides new-guides))] + (rx/of (dwc/commit-changes changes)))))) (defn move-frame-guides "Move guides that are inside a frame when that frame is moved" @@ -78,7 +62,10 @@ ptk/WatchEvent (watch [_ state _] (let [objects (wsh/lookup-page-objects state) - frame-ids? (->> ids (filter #(= :frame (get-in objects [% :type]))) (into #{})) + + is-frame? (fn [id] (= :frame (get-in objects [id :type]))) + frame-ids? (into #{} (filter is-frame?) ids) + object-modifiers (get state :workspace-modifiers) build-move-event diff --git a/frontend/src/app/main/data/workspace/state_helpers.cljs b/frontend/src/app/main/data/workspace/state_helpers.cljs index f144670ba..661b272ad 100644 --- a/frontend/src/app/main/data/workspace/state_helpers.cljs +++ b/frontend/src/app/main/data/workspace/state_helpers.cljs @@ -9,6 +9,12 @@ [app.common.data :as d] [app.common.pages :as cp])) +(defn lookup-page + ([state] + (lookup-page state (:current-page-id state))) + ([state page-id] + (get-in state [:workspace-data :pages-index page-id]))) + (defn lookup-page-objects ([state] (lookup-page-objects state (:current-page-id state))) diff --git a/frontend/src/app/main/ui/workspace/viewport/snap_points.cljs b/frontend/src/app/main/ui/workspace/viewport/snap_points.cljs index ee807765f..d36109911 100644 --- a/frontend/src/app/main/ui/workspace/viewport/snap_points.cljs +++ b/frontend/src/app/main/ui/workspace/viewport/snap_points.cljs @@ -152,10 +152,7 @@ {::mf/wrap [mf/memo]} [{:keys [layout zoom objects selected page-id drawing transform modifiers] :as props}] - (let [shapes - (->> selected - (map #(get objects %)) - (filterv (comp not nil?))) + (let [shapes (into [] (keep (d/getf objects)) selected) filter-shapes (into #{} diff --git a/frontend/src/app/util/snap_data.cljs b/frontend/src/app/util/snap_data.cljs index 9a58cd1b2..6c667c217 100644 --- a/frontend/src/app/util/snap_data.cljs +++ b/frontend/src/app/util/snap_data.cljs @@ -22,22 +22,28 @@ ;; PRIVATE FUNCTIONS (defn- make-insert-tree-data + "Inserts all data in it's corresponding axis bucket" [shape-data axis] (fn [tree] - (let [tree (or tree (rt/make-tree))] - (as-> tree $ - (reduce (fn [tree data] - (rt/insert tree (get-in data [:pt axis]) data)) - $ shape-data))))) + (let [tree (or tree (rt/make-tree)) + + insert-data + (fn [tree data] + (rt/insert tree (get-in data [:pt axis]) data))] + + (reduce insert-data tree shape-data)))) (defn- make-delete-tree-data + "Removes all data in it's corresponding axis bucket" [shape-data axis] (fn [tree] - (let [tree (or tree (rt/make-tree))] - (as-> tree $ - (reduce (fn [tree data] - (rt/remove tree (get-in data [:pt axis]) data)) - $ shape-data))))) + (let [tree (or tree (rt/make-tree)) + + remove-data + (fn [tree data] + (rt/remove tree (get-in data [:pt axis]) data))] + + (reduce remove-data tree shape-data)))) (defn- add-root-frame [page-data] @@ -52,19 +58,19 @@ (let [frame-id (:id frame) parent-id (:parent-id frame) frame-data (->> (snap/shape-snap-points frame) - (map #(hash-map :type :shape - :id frame-id - :pt %))) + (mapv #(array-map :type :shape + :id frame-id + :pt %))) grid-x-data (->> (gg/grid-snap-points frame :x) - (map #(hash-map :type :grid-x - :id frame-id - :pt %))) + (mapv #(array-map :type :grid-x + :id frame-id + :pt %))) grid-y-data (->> (gg/grid-snap-points frame :y) - (map #(hash-map :type :grid-y - :id frame-id - :pt %)))] + (mapv #(array-map :type :grid-y + :id frame-id + :pt %)))] (-> page-data ;; Update root frame information @@ -84,7 +90,7 @@ (let [frame-id (:frame-id shape) snap-points (snap/shape-snap-points shape) shape-data (->> snap-points - (mapv #(hash-map + (mapv #(array-map :type :shape :id (:id shape) :pt %)))] @@ -98,7 +104,7 @@ [page-data guide] (let [guide-data (->> (snap/guide-snap-points guide) - (mapv #(hash-map + (mapv #(array-map :type :guide :id (:id guide) :pt %)))] diff --git a/frontend/src/app/worker/snaps.cljs b/frontend/src/app/worker/snaps.cljs index b992b51e7..da872c2f7 100644 --- a/frontend/src/app/worker/snaps.cljs +++ b/frontend/src/app/worker/snaps.cljs @@ -28,9 +28,10 @@ (defmethod impl/handler :snaps/range-query [{:keys [page-id frame-id axis ranges] :as message}] - (->> ranges - (mapcat #(sd/query @state page-id frame-id axis %)) - (set) ;; unique - (into []))) + + (into [] + (comp (mapcat #(sd/query @state page-id frame-id axis %)) + (distinct)) + ranges))