0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-24 23:49:45 -05:00

🐛 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

@ -52,7 +52,7 @@
(t/testing "Shape without modifiers should stay the same"
(t/are [type]
(let [shape-before (create-test-shape type)
shape-after (gsh/transform-shape shape-before)]
shape-after (gsh/transform-shape shape-before {:round-coords? false})]
(= shape-before shape-after))
:rect :path))
@ -61,7 +61,7 @@
(t/are [type]
(let [modifiers {:displacement (gmt/translate-matrix (gpt/point 10 -10))}]
(let [shape-before (create-test-shape type {:modifiers modifiers})
shape-after (gsh/transform-shape shape-before)]
shape-after (gsh/transform-shape shape-before {:round-coords? false})]
(t/is (not= shape-before shape-after))
(t/is (close? (get-in shape-before [:selrect :x])
@ -82,7 +82,7 @@
(t/are [type]
(let [modifiers {:displacement (gmt/matrix)}
shape-before (create-test-shape type {:modifiers modifiers})
shape-after (gsh/transform-shape shape-before)]
shape-after (gsh/transform-shape shape-before {:round-coords? false})]
(t/are [prop]
(t/is (close? (get-in shape-before [:selrect prop])
(get-in shape-after [:selrect prop])))
@ -95,7 +95,7 @@
:resize-vector (gpt/point 2 2)
:resize-transform (gmt/matrix)}
shape-before (create-test-shape type {:modifiers modifiers})
shape-after (gsh/transform-shape shape-before)]
shape-after (gsh/transform-shape shape-before {:round-coords? false})]
(t/is (not= shape-before shape-after))
(t/is (close? (get-in shape-before [:selrect :x])
@ -117,7 +117,7 @@
:resize-vector (gpt/point 1 1)
:resize-transform (gmt/matrix)}
shape-before (create-test-shape type {:modifiers modifiers})
shape-after (gsh/transform-shape shape-before)]
shape-after (gsh/transform-shape shape-before {:round-coords? false})]
(t/are [prop]
(t/is (close? (get-in shape-before [:selrect prop])
(get-in shape-after [:selrect prop])))
@ -130,7 +130,7 @@
:resize-vector (gpt/point 0 0)
:resize-transform (gmt/matrix)}
shape-before (create-test-shape type {:modifiers modifiers})
shape-after (gsh/transform-shape shape-before)]
shape-after (gsh/transform-shape shape-before {:round-coords? false})]
(t/is (> (get-in shape-before [:selrect :width])
(get-in shape-after [:selrect :width])))
(t/is (> (get-in shape-after [:selrect :width]) 0))
@ -144,7 +144,7 @@
(t/are [type]
(let [modifiers {:rotation 30}
shape-before (create-test-shape type {:modifiers modifiers})
shape-after (gsh/transform-shape shape-before)]
shape-after (gsh/transform-shape shape-before {:round-coords? false})]
(t/is (not= shape-before shape-after))
@ -168,7 +168,7 @@
(t/are [type]
(let [modifiers {:rotation 0}
shape-before (create-test-shape type {:modifiers modifiers})
shape-after (gsh/transform-shape shape-before)]
shape-after (gsh/transform-shape shape-before {:round-coords? false})]
(t/are [prop]
(t/is (close? (get-in shape-before [:selrect prop])
(get-in shape-after [:selrect prop])))
@ -180,7 +180,7 @@
(let [modifiers {:displacement (gmt/matrix)}
shape-before (-> (create-test-shape type {:modifiers modifiers})
(assoc :selrect selrect))
shape-after (gsh/transform-shape shape-before)]
shape-after (gsh/transform-shape shape-before {:round-coords? false})]
(= (:selrect shape-before) (:selrect shape-after)))
:rect {:x 0 :y 0 :width ##Inf :height ##Inf}

View file

@ -266,7 +266,7 @@
(defn apply-transform
"Given a new set of points transformed, set up the rectangle so it keeps
its properties. We adjust de x,y,width,height and create a custom transform"
[shape transform]
[shape transform round-coords?]
;;
(let [points (-> shape :points (transform-points transform))
center (gco/center-points points)
@ -283,6 +283,11 @@
center
(:width points-temp-dim)
(:height points-temp-dim))
(cond-> round-coords?
(-> (update :x #(mth/precision % 0))
(update :y #(mth/precision % 0))
(update :width #(mth/precision % 0))
(update :height #(mth/precision % 0))))
(update :width max 1)
(update :height max 1))
@ -297,11 +302,7 @@
:else
(-> shape
(merge rect-shape)
(update :x #(mth/precision % 0))
(update :y #(mth/precision % 0))
(update :width #(mth/precision % 0))
(update :height #(mth/precision % 0))))]
(merge rect-shape)))]
(as-> shape $
(update $ :transform #(gmt/multiply (or % (gmt/matrix)) matrix))
(update $ :transform-inverse #(gmt/multiply matrix-inverse (or % (gmt/matrix))))
@ -347,18 +348,23 @@
%)))
shape))
(defn transform-shape [shape]
(let [shape (apply-displacement shape)
center (gco/center-shape shape)
modifiers (:modifiers shape)]
(if (and modifiers center)
(let [transform (modifiers->transform center modifiers)]
(-> shape
(set-flip modifiers)
(apply-transform transform)
(apply-text-resize shape modifiers)
(dissoc :modifiers)))
shape)))
(defn transform-shape
([shape]
(transform-shape shape nil))
([shape {:keys [round-coords?]
:or {round-coords? true}}]
(let [shape (apply-displacement shape)
center (gco/center-shape shape)
modifiers (:modifiers shape)]
(if (and modifiers center)
(let [transform (modifiers->transform center modifiers)]
(-> shape
(set-flip modifiers)
(apply-transform transform round-coords?)
(apply-text-resize shape modifiers)
(dissoc :modifiers)))
shape))))
(defn update-group-viewbox
"Updates the viewbox for groups imported from SVG's"
@ -407,5 +413,5 @@
;; need to remove the flip flags
(assoc :flip-x false)
(assoc :flip-y false)
(apply-transform (gmt/matrix)))))
(apply-transform (gmt/matrix) true))))

View file

@ -461,8 +461,9 @@
(defn merge-modifiers
[objects modifiers]
(let [set-modifier (fn [objects [id modifiers]]
(-> objects
(d/update-when id assoc :modifiers modifiers)))]
(let [set-modifier
(fn [objects [id modifiers]]
(-> objects
(d/update-when id merge modifiers)))]
(->> modifiers
(reduce set-modifier objects))))

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)