0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-09 08:20:45 -05:00

🐛 Fix problem with alignment

This commit is contained in:
alonso.torres 2023-11-02 15:17:28 +01:00 committed by Andrés Moya
parent 64a566a0f6
commit ffcec9ec03

View file

@ -6,8 +6,9 @@
(ns app.common.geom.align
(:require
[app.common.geom.point :as gpt]
[app.common.geom.rect :as grc]
[app.common.geom.shapes :as gsh] ))
[app.common.geom.shapes :as gsh]))
;; --- Alignment
@ -25,8 +26,8 @@
[shape rect axis]
(let [wrapper-rect (gsh/shapes->rect [shape])
align-pos (calc-align-pos wrapper-rect rect axis)
delta {:x (- (:x align-pos) (:x wrapper-rect))
:y (- (:y align-pos) (:y wrapper-rect))}]
delta (gpt/point (- (:x align-pos) (:x wrapper-rect))
(- (:y align-pos) (:y wrapper-rect)))]
(gsh/move shape delta)))
(defn calc-align-pos
@ -70,33 +71,35 @@
(let [coord (if (= axis :horizontal) :x :y)
other-coord (if (= axis :horizontal) :y :x)
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)
; 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)
; Each shape wrapped in its own rectangle
;; Each shape wrapped in its own rectangle
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))
unit-space (/ space (- (count wrapped-shapes) 1))
;; Calculate the distance we need to move each shape.
;; The new position of each one is the position of the
;; previous one plus its size plus the unit space.
deltas (loop [shapes' wrapped-shapes
start-pos (coord wrapper-rect)
deltas []]
deltas
(loop [shapes' wrapped-shapes
start-pos (coord wrapper-rect)
deltas []]
(let [first-shape (first shapes')
delta (- start-pos (coord first-shape))
new-pos (+ start-pos (size first-shape) unit-space)]
(let [first-shape (first shapes')
delta (- start-pos (coord first-shape))
new-pos (+ start-pos (size first-shape) unit-space)]
(if (= (count shapes') 1)
(conj deltas delta)
(recur (rest shapes')
new-pos
(conj deltas delta)))))]
(if (= (count shapes') 1)
(conj deltas delta)
(recur (rest shapes')
new-pos
(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)))
;; Adjust to viewport