🐛 Fix problems with snap index regeneration

This commit is contained in:
alonso.torres 2021-05-14 15:38:04 +02:00 committed by Andrey Antukh
parent b2e01cd52b
commit 93d8c171be
12 changed files with 118 additions and 53 deletions

View file

@ -6,7 +6,8 @@
(ns app.main.data.workspace.state-helpers
(:require
[app.common.data :as d]))
[app.common.data :as d]
[app.common.pages :as cp]))
(defn lookup-page-objects
([state]
@ -26,8 +27,9 @@
(defn lookup-selected
[state]
(let [selected (get-in state [:workspace-local :selected])
objects (lookup-page-objects state)
(let [objects (lookup-page-objects state)
selected (->> (get-in state [:workspace-local :selected])
(cp/clean-loops objects))
is-present? (fn [id] (contains? objects id))]
(into (d/ordered-set)
(filter is-present?)

View file

@ -72,6 +72,27 @@
:bottom-left [ex sy])]
(gpt/point x y)))
(defn- fix-init-point
"Fix the initial point so the resizes are accurate"
[initial handler shape]
(let [{:keys [x y width height]} (:selrect shape)
{:keys [rotation]} shape
rotation (or rotation 0)]
(if (= rotation 0)
(cond-> initial
(contains? #{:left :top-left :bottom-left} handler)
(assoc :x x)
(contains? #{:right :top-right :bottom-right} handler)
(assoc :x (+ x width))
(contains? #{:top :top-right :top-left} handler)
(assoc :y y)
(contains? #{:bottom :bottom-right :bottom-left} handler)
(assoc :y (+ y height)))
initial)))
(defn finish-transform []
(ptk/reify ::finish-transform
ptk/UpdateEvent
@ -84,6 +105,10 @@
(letfn [(resize [shape initial resizing-shapes layout [point lock? point-snap]]
(let [{:keys [width height]} (:selrect shape)
{:keys [rotation]} shape
rotation (or rotation 0)
initial (fix-init-point initial handler shape)
shapev (-> (gpt/point width height))
scale-text (:scale-text layout)
@ -253,6 +278,7 @@
frame-id (cp/frame-id-by-position objects position)
moving-shapes (->> ids
(cp/clean-loops objects)
(map #(get objects %))
(remove #(= (:frame-id %) frame-id)))

View file

@ -72,7 +72,7 @@
[props]
(let [shape (obj/get props "shape")
frame (obj/get props "frame")
shape (-> (geom/transform-shape shape)
shape (-> (geom/transform-shape shape {:round-coords? false})
(geom/translate-to-frame frame))
opts #js {:shape shape
:frame frame}

View file

@ -7,6 +7,7 @@
(ns app.main.ui.workspace.viewport
(:require
[app.common.data :as d]
[app.common.pages :as cp]
[app.common.geom.shapes :as gsh]
[app.main.refs :as refs]
[app.main.ui.context :as ctx]
@ -61,7 +62,9 @@
options (mf/deref refs/workspace-page-options)
objects (mf/deref refs/workspace-page-objects)
object-modifiers (mf/deref refs/workspace-modifiers)
objects (d/deep-merge objects object-modifiers)
objects (mf/use-memo
(mf/deps objects object-modifiers)
#(cp/merge-modifiers objects object-modifiers))
;; STATE
alt? (mf/use-state false)
@ -135,7 +138,9 @@
show-prototypes? (= options-mode :prototype)
show-selection-handlers? (seq selected)
show-snap-distance? (and (contains? layout :dynamic-alignment) (= transform :move) (not (empty? selected)))
show-snap-points? (and (contains? layout :dynamic-alignment) (or drawing-obj transform))
show-snap-points? (and (or (contains? layout :dynamic-alignment)
(contains? layout :snap-grid))
(or drawing-obj transform))
show-selrect? (and selrect (empty? drawing))
show-measures? (and (not transform) (not path-editing?) show-distances?)]
@ -283,6 +288,7 @@
:zoom zoom
:page-id page-id
:selected selected
:objects objects
:modifiers modifiers}])
(when show-snap-distance?

View file

@ -29,7 +29,7 @@
(mf/deps shape)
#(when path? (upf/format-path (:content shape))))
{:keys [id x y width height]} shape
{:keys [id x y width height selrect]} shape
outline-type (case (:type shape)
:circle "ellipse"
@ -53,10 +53,10 @@
{:d path-data
:transform nil}
{:x x
:y y
:width width
:height height})]
{:x (:x selrect)
:y (:y selrect)
:width (:width selrect)
:height (:height selrect)})]
[:> outline-type (map->obj (merge common props))]))

View file

@ -241,8 +241,7 @@
disable-handlers (obj/get props "disable-handlers")
current-transform (mf/deref refs/current-transform)
selrect (-> (:selrect shape)
minimum-selrect)
selrect (:selrect shape)
transform (geom/transform-matrix shape {:no-flip true})]
(when (not (#{:move :rotate} current-transform))
@ -327,7 +326,7 @@
(mf/defc single-selection-handlers
[{:keys [shape zoom color disable-handlers on-move-selected] :as props}]
(let [shape-id (:id shape)
shape (geom/transform-shape shape)
shape (geom/transform-shape shape {:round-coords? false})
frame (mf/deref (refs/object-by-id (:frame-id shape)))
frame (when-not (= (:id frame) uuid/zero) frame)

View file

@ -6,6 +6,7 @@
(ns app.main.ui.workspace.viewport.snap-points
(:require
[app.common.pages :as cp]
[app.common.math :as mth]
[app.common.data :as d]
[app.common.geom.point :as gpt]
@ -151,15 +152,25 @@
(mf/defc snap-points
{::mf/wrap [mf/memo]}
[{:keys [layout zoom selected page-id drawing transform modifiers] :as props}]
(let [shapes (mf/deref (refs/objects-by-id selected))
filter-shapes (mf/deref refs/selected-shapes-with-children)
[{:keys [layout zoom objects selected page-id drawing transform modifiers] :as props}]
(let [;; shapes (mf/deref (refs/objects-by-id selected))
;; filter-shapes (mf/deref refs/selected-shapes-with-children)
shapes (->> selected
(map #(get objects %))
(filterv (comp not nil?)))
filter-shapes (into #{}
(mapcat #(cp/get-object-with-children % objects))
selected)
filter-shapes (fn [id]
(if (= id :layout)
(or (not (contains? layout :display-grid))
(not (contains? layout :snap-grid)))
(or (filter-shapes id)
(not (contains? layout :dynamic-alignment)))))
shapes (if drawing [drawing] shapes)]
(when (or drawing transform)
[:& snap-feedback {:shapes shapes

View file

@ -88,7 +88,9 @@
(defn grid-snap-points
"Returns the snap points for a given grid"
([shape coord] (mapcat #(grid-snap-points shape % coord) (:grids shape)))
([shape coord]
(mapcat #(grid-snap-points shape % coord) (:grids shape)))
([shape {:keys [type display params] :as grid} coord]
(when (:display grid)
(case type

View file

@ -71,13 +71,23 @@
(fn [frame-id shapes]
{:x (-> (rt/make-tree) (add-coord-data frame-id shapes :x))
:y (-> (rt/make-tree) (add-coord-data frame-id shapes :y))})]
(d/mapm create-index shapes-data)))
;; Attributes that will change the values of their snap
(def snap-attrs [:x :y :width :height :selrect :grids])
(defn- update-snap-data
[snap-data old-objects new-objects]
(let [changed? #(not= (get old-objects %) (get new-objects %))
(let [changed? (fn [id]
(let [oldv (get old-objects id)
newv (get new-objects id)]
;; Check first without select-keys because is faster if they are
;; the same reference
(and (not= oldv newv)
(not= (select-keys oldv snap-attrs)
(select-keys newv snap-attrs)))))
is-deleted-frame? #(and (not= uuid/zero %)
(contains? old-objects %)
(not (contains? new-objects %))
@ -119,9 +129,9 @@
:y (rt/make-tree)}))]
(as-> snap-data $
(reduce delete-data $ to-delete)
(reduce add-frames $ frames-to-add)
(reduce add-data $ to-add)
(reduce delete-data $ to-delete)
(reduce delete-frames $ frames-to-delete))))
(defn- log-state
@ -160,8 +170,10 @@
(defmethod impl/handler :snaps/update-index
[{:keys [page-id old-objects new-objects] :as message}]
;; TODO: Check the difference and update the index acordingly
(swap! state update-page page-id old-objects new-objects)
;; Uncomment this to regenerate the index everytime
#_(swap! state index-page page-id new-objects)
;; (log-state)
nil)