0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-13 08:11:30 -05:00

🎉 Update relative position of shapes inside component

This commit is contained in:
Andrés Moya 2020-09-16 11:37:54 +02:00
parent 47a8da43dc
commit 63c9e80ed4

View file

@ -353,6 +353,7 @@
(declare remove-ref) (declare remove-ref)
(declare update-attrs) (declare update-attrs)
(declare sync-attrs) (declare sync-attrs)
(declare calc-new-pos)
(defn reset-component (defn reset-component
[id] [id]
@ -465,7 +466,8 @@
[root-shape page components] [root-shape page components]
(let [objects (get page :objects) (let [objects (get page :objects)
all-shapes (cph/get-object-with-children (:id root-shape) objects) all-shapes (cph/get-object-with-children (:id root-shape) objects)
component (get components (:component-id root-shape))] component (get components (:component-id root-shape))
root-component (get-in component [:objects (:shape-ref root-shape)])]
(loop [shapes (seq all-shapes) (loop [shapes (seq all-shapes)
rchanges [] rchanges []
uchanges []] uchanges []]
@ -473,19 +475,19 @@
(if (nil? shape) (if (nil? shape)
[rchanges uchanges] [rchanges uchanges]
(let [[shape-rchanges shape-uchanges] (let [[shape-rchanges shape-uchanges]
(generate-sync-shape shape page component)] (generate-sync-shape shape root-shape root-component page component)]
(recur (next shapes) (recur (next shapes)
(concat rchanges shape-rchanges) (concat rchanges shape-rchanges)
(concat uchanges shape-uchanges)))))))) (concat uchanges shape-uchanges))))))))
(defn- generate-sync-shape (defn- generate-sync-shape
[shape page component] [shape root-shape root-component page component]
(if (nil? component) (if (nil? component)
(remove-component-and-ref shape page) (remove-component-and-ref shape page)
(let [component-shape (get (:objects component) (:shape-ref shape))] (let [component-shape (get (:objects component) (:shape-ref shape))]
(if (nil? component-shape) (if (nil? component-shape)
(remove-ref shape page) (remove-ref shape page)
(update-attrs shape component-shape page))))) (update-attrs shape component-shape root-shape root-component page)))))
(defn- remove-component-and-ref (defn- remove-component-and-ref
[shape page] [shape page]
@ -530,10 +532,22 @@
:val (:shape-ref shape)}]}]]) :val (:shape-ref shape)}]}]])
(defn- update-attrs (defn- update-attrs
[shape component-shape page] [shape component-shape root-shape root-component page]
(let [new-pos (calc-new-pos shape component-shape root-shape root-component)]
(loop [attrs (seq sync-attrs) (loop [attrs (seq sync-attrs)
roperations [] roperations [{:type :set
uoperations []] :attr :x
:val (:x new-pos)}
{:type :set
:attr :y
:val (:y new-pos)}]
uoperations [{:type :set
:attr :x
:val (:x shape)}
{:type :set
:attr :y
:val (:y shape)}]]
(let [attr (first attrs)] (let [attr (first attrs)]
(if (nil? attr) (if (nil? attr)
(let [rchanges [{:type :mod-obj (let [rchanges [{:type :mod-obj
@ -557,7 +571,7 @@
:val (get shape attr)}] :val (get shape attr)}]
(recur (next attrs) (recur (next attrs)
(conj roperations roperation) (conj roperations roperation)
(conj uoperations uoperation)))))))) (conj uoperations uoperation)))))))))
(def sync-attrs [:content (def sync-attrs [:content
:fill-color :fill-color
@ -584,5 +598,16 @@
:width :width
:height :height
:interactions :interactions
:points]) :points
:transform])
(defn- calc-new-pos
[shape component-shape root-shape root-component]
(let [root-pos (gpt/point (:x root-shape) (:y root-shape))
root-component-pos (gpt/point (:x root-component) (:y root-component))
component-pos (gpt/point (:x component-shape) (:y component-shape))
delta (gpt/subtract component-pos root-component-pos)
shape-pos (gpt/point (:x shape) (:y shape))
new-pos (gpt/add root-pos delta)]
new-pos))