mirror of
https://github.com/penpot/penpot.git
synced 2025-01-26 00:19:07 -05:00
🐛 Fix problem with alignment
This commit is contained in:
parent
64a566a0f6
commit
ffcec9ec03
1 changed files with 22 additions and 19 deletions
|
@ -6,8 +6,9 @@
|
||||||
|
|
||||||
(ns app.common.geom.align
|
(ns app.common.geom.align
|
||||||
(:require
|
(:require
|
||||||
|
[app.common.geom.point :as gpt]
|
||||||
[app.common.geom.rect :as grc]
|
[app.common.geom.rect :as grc]
|
||||||
[app.common.geom.shapes :as gsh] ))
|
[app.common.geom.shapes :as gsh]))
|
||||||
|
|
||||||
;; --- Alignment
|
;; --- Alignment
|
||||||
|
|
||||||
|
@ -25,8 +26,8 @@
|
||||||
[shape rect axis]
|
[shape rect axis]
|
||||||
(let [wrapper-rect (gsh/shapes->rect [shape])
|
(let [wrapper-rect (gsh/shapes->rect [shape])
|
||||||
align-pos (calc-align-pos wrapper-rect rect axis)
|
align-pos (calc-align-pos wrapper-rect rect axis)
|
||||||
delta {:x (- (:x align-pos) (:x wrapper-rect))
|
delta (gpt/point (- (:x align-pos) (:x wrapper-rect))
|
||||||
:y (- (:y align-pos) (:y wrapper-rect))}]
|
(- (:y align-pos) (:y wrapper-rect)))]
|
||||||
(gsh/move shape delta)))
|
(gsh/move shape delta)))
|
||||||
|
|
||||||
(defn calc-align-pos
|
(defn calc-align-pos
|
||||||
|
@ -70,33 +71,35 @@
|
||||||
(let [coord (if (= axis :horizontal) :x :y)
|
(let [coord (if (= axis :horizontal) :x :y)
|
||||||
other-coord (if (= axis :horizontal) :y :x)
|
other-coord (if (= axis :horizontal) :y :x)
|
||||||
size (if (= axis :horizontal) :width :height)
|
size (if (= axis :horizontal) :width :height)
|
||||||
; The rectangle that wraps the whole selection
|
;; The rectangle that wraps the whole selection
|
||||||
wrapper-rect (gsh/shapes->rect shapes)
|
wrapper-rect (gsh/shapes->rect shapes)
|
||||||
; Sort shapes by the center point in the given axis
|
;; Sort shapes by the center point in the given axis
|
||||||
sorted-shapes (sort-by #(coord (gsh/shape->center %)) shapes)
|
sorted-shapes (sort-by #(coord (gsh/shape->center %)) shapes)
|
||||||
; Each shape wrapped in its own rectangle
|
;; Each shape wrapped in its own rectangle
|
||||||
wrapped-shapes (map #(gsh/shapes->rect [%]) sorted-shapes)
|
wrapped-shapes (map #(gsh/shapes->rect [%]) sorted-shapes)
|
||||||
; The total space between shapes
|
;; The total space between shapes
|
||||||
space (reduce - (size wrapper-rect) (map size wrapped-shapes))
|
space (reduce - (size wrapper-rect) (map size wrapped-shapes))
|
||||||
unit-space (/ space (- (count wrapped-shapes) 1))
|
unit-space (/ space (- (count wrapped-shapes) 1))
|
||||||
|
|
||||||
;; Calculate the distance we need to move each shape.
|
;; Calculate the distance we need to move each shape.
|
||||||
;; The new position of each one is the position of the
|
;; The new position of each one is the position of the
|
||||||
;; previous one plus its size plus the unit space.
|
;; previous one plus its size plus the unit space.
|
||||||
deltas (loop [shapes' wrapped-shapes
|
deltas
|
||||||
start-pos (coord wrapper-rect)
|
(loop [shapes' wrapped-shapes
|
||||||
deltas []]
|
start-pos (coord wrapper-rect)
|
||||||
|
deltas []]
|
||||||
|
|
||||||
(let [first-shape (first shapes')
|
(let [first-shape (first shapes')
|
||||||
delta (- start-pos (coord first-shape))
|
delta (- start-pos (coord first-shape))
|
||||||
new-pos (+ start-pos (size first-shape) unit-space)]
|
new-pos (+ start-pos (size first-shape) unit-space)]
|
||||||
|
|
||||||
(if (= (count shapes') 1)
|
(if (= (count shapes') 1)
|
||||||
(conj deltas delta)
|
(conj deltas delta)
|
||||||
(recur (rest shapes')
|
(recur (rest shapes')
|
||||||
new-pos
|
new-pos
|
||||||
(conj deltas delta)))))]
|
(conj deltas delta)))))]
|
||||||
|
|
||||||
(map #(gsh/move %1 {coord %2 other-coord 0})
|
(map #(gsh/move %1 (assoc (gpt/point) coord %2 other-coord 0))
|
||||||
sorted-shapes deltas)))
|
sorted-shapes deltas)))
|
||||||
|
|
||||||
;; Adjust to viewport
|
;; Adjust to viewport
|
||||||
|
|
Loading…
Add table
Reference in a new issue