Review changes

This commit is contained in:
alonso.torres 2022-01-25 14:48:54 +01:00
parent 8833e19c7f
commit 6e0433a34b
6 changed files with 91 additions and 70 deletions

View file

@ -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}))))

View file

@ -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

View file

@ -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)))

View file

@ -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 #{}

View file

@ -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 %)))]

View file

@ -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))