mirror of
https://github.com/penpot/penpot.git
synced 2025-07-28 19:37:19 +02:00
✨ Improved snap to grids
This commit is contained in:
parent
b5e965cf1a
commit
aec68c52ab
15 changed files with 220 additions and 119 deletions
|
@ -168,3 +168,10 @@
|
|||
nm (str/trim (str/lower name))]
|
||||
(str/includes? nm st))))
|
||||
|
||||
(defn tap
|
||||
"Works like rx/tap but for normal collections"
|
||||
;; Signature for transducer use
|
||||
([f]
|
||||
(map #(do (f %) %)))
|
||||
([f coll]
|
||||
(map #(do (f %) %) coll)))
|
||||
|
|
|
@ -46,7 +46,7 @@
|
|||
next-v (fn [cur-val]
|
||||
(+ offset v (* (+ width' gutter) cur-val)))]
|
||||
|
||||
[size width' next-v]))
|
||||
[size width' next-v gutter]))
|
||||
|
||||
(defn- calculate-column-grid
|
||||
[{:keys [width height x y] :as frame} params]
|
||||
|
@ -70,6 +70,20 @@
|
|||
|
||||
[(* col-size row-size) size size next-x next-y]))
|
||||
|
||||
(defn grid-gutter
|
||||
[{:keys [x y width height]} {:keys [type params] :as grid}]
|
||||
|
||||
(case type
|
||||
:column
|
||||
(let [[_ _ _ gutter] (calculate-generic-grid x width params)]
|
||||
gutter)
|
||||
|
||||
:row
|
||||
(let [[_ _ _ gutter] (calculate-generic-grid y height params)]
|
||||
gutter)
|
||||
|
||||
nil))
|
||||
|
||||
(defn grid-areas
|
||||
"Given a frame and the grid parameters returns the areas defined on the grid"
|
||||
[frame grid]
|
||||
|
@ -93,28 +107,25 @@
|
|||
|
||||
(defn grid-snap-points
|
||||
"Returns the snap points for a given grid"
|
||||
([shape coord]
|
||||
(mapcat #(grid-snap-points shape % coord) (:grids shape)))
|
||||
[shape {:keys [type params] :as grid} coord]
|
||||
(when (:display grid)
|
||||
(case type
|
||||
:square
|
||||
(let [{:keys [x y width height]} shape
|
||||
size (-> params :size)]
|
||||
(when (> size 0)
|
||||
(if (= coord :x)
|
||||
(mapcat #(vector (gpt/point (+ x %) y)
|
||||
(gpt/point (+ x %) (+ y height))) (range size width size))
|
||||
(mapcat #(vector (gpt/point x (+ y %))
|
||||
(gpt/point (+ x width) (+ y %))) (range size height size)))))
|
||||
|
||||
([shape {:keys [type params] :as grid} coord]
|
||||
(when (:display grid)
|
||||
(case type
|
||||
:square
|
||||
(let [{:keys [x y width height]} shape
|
||||
size (-> params :size)]
|
||||
(when (> size 0)
|
||||
(if (= coord :x)
|
||||
(mapcat #(vector (gpt/point (+ x %) y)
|
||||
(gpt/point (+ x %) (+ y height))) (range size width size))
|
||||
(mapcat #(vector (gpt/point x (+ y %))
|
||||
(gpt/point (+ x width) (+ y %))) (range size height size)))))
|
||||
:column
|
||||
(when (= coord :x)
|
||||
(->> (grid-areas shape grid)
|
||||
(mapcat grid-area-points)))
|
||||
|
||||
:column
|
||||
(when (= coord :x)
|
||||
(->> (grid-areas shape grid)
|
||||
(mapcat grid-area-points)))
|
||||
|
||||
:row
|
||||
(when (= coord :y)
|
||||
(->> (grid-areas shape grid)
|
||||
(mapcat grid-area-points)))))))
|
||||
:row
|
||||
(when (= coord :y)
|
||||
(->> (grid-areas shape grid)
|
||||
(mapcat grid-area-points))))))
|
||||
|
|
|
@ -9,25 +9,28 @@
|
|||
[app.common.geom.point :as gpt]
|
||||
[app.common.geom.shapes :as gsh]))
|
||||
|
||||
(defn selrect-snap-points [{:keys [x y width height]}]
|
||||
(defn selrect-snap-points [{:keys [x y width height] :as selrect}]
|
||||
#{(gpt/point x y)
|
||||
(gpt/point (+ x width) y)
|
||||
(gpt/point (+ x width) (+ y height))
|
||||
(gpt/point x (+ y height))})
|
||||
(gpt/point x (+ y height))
|
||||
(gsh/center-selrect selrect)})
|
||||
|
||||
(defn frame-snap-points [{:keys [x y width height] :as selrect}]
|
||||
(into (selrect-snap-points selrect)
|
||||
#{(gpt/point (+ x (/ width 2)) y)
|
||||
(gpt/point (+ x width) (+ y (/ height 2)))
|
||||
(gpt/point (+ x (/ width 2)) (+ y height))
|
||||
(gpt/point x (+ y (/ height 2)))}))
|
||||
(defn frame-snap-points [{:keys [x y width height blocked hidden] :as selrect}]
|
||||
(when (and (not blocked) (not hidden))
|
||||
(into (selrect-snap-points selrect)
|
||||
#{(gpt/point (+ x (/ width 2)) y)
|
||||
(gpt/point (+ x width) (+ y (/ height 2)))
|
||||
(gpt/point (+ x (/ width 2)) (+ y height))
|
||||
(gpt/point x (+ y (/ height 2)))})))
|
||||
|
||||
(defn shape-snap-points
|
||||
[shape]
|
||||
(let [shape (gsh/transform-shape shape)]
|
||||
(case (:type shape)
|
||||
:frame (-> shape :selrect frame-snap-points)
|
||||
(into #{(gsh/center-shape shape)} (:points shape)))))
|
||||
[{:keys [hidden blocked] :as shape}]
|
||||
(when (and (not blocked) (not hidden))
|
||||
(let [shape (gsh/transform-shape shape)]
|
||||
(case (:type shape)
|
||||
:frame (-> shape :selrect frame-snap-points)
|
||||
(into #{(gsh/center-shape shape)} (:points shape))))))
|
||||
|
||||
(defn guide-snap-points
|
||||
[guide]
|
||||
|
|
|
@ -53,6 +53,19 @@
|
|||
(assoc-in [frame-id :x] (rt/make-tree))
|
||||
(assoc-in [frame-id :y] (rt/make-tree)))))
|
||||
|
||||
(defn get-grids-snap-points
|
||||
[frame coord]
|
||||
(let [grid->snap (fn [[grid-type position]]
|
||||
{:type :layout
|
||||
:id (:id frame)
|
||||
:grid grid-type
|
||||
:pt position})]
|
||||
(->> (:grids frame)
|
||||
(mapcat (fn [grid]
|
||||
(->> (gg/grid-snap-points frame grid coord)
|
||||
(mapv #(vector (:type grid) %)))))
|
||||
(mapv grid->snap))))
|
||||
|
||||
(defn- add-frame
|
||||
[page-data frame]
|
||||
(let [frame-id (:id frame)
|
||||
|
@ -61,17 +74,8 @@
|
|||
(mapv #(array-map :type :shape
|
||||
:id frame-id
|
||||
:pt %)))
|
||||
|
||||
grid-x-data (->> (gg/grid-snap-points frame :x)
|
||||
(mapv #(array-map :type :layout
|
||||
:id frame-id
|
||||
:pt %)))
|
||||
|
||||
grid-y-data (->> (gg/grid-snap-points frame :y)
|
||||
(mapv #(array-map :type :layout
|
||||
:id frame-id
|
||||
:pt %)))]
|
||||
|
||||
grid-x-data (get-grids-snap-points frame :x)
|
||||
grid-y-data (get-grids-snap-points frame :y)]
|
||||
(-> page-data
|
||||
;; Update root frame information
|
||||
(assoc-in [uuid/zero :objects-data frame-id] frame-data)
|
||||
|
@ -107,6 +111,7 @@
|
|||
(mapv #(array-map
|
||||
:type :guide
|
||||
:id (:id guide)
|
||||
:axis (:axis guide)
|
||||
:frame-id (:frame-id guide)
|
||||
:pt %)))]
|
||||
(if-let [frame-id (:frame-id guide)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue