From 44ca01aa273e57314132695105afd8d6ce8af2d3 Mon Sep 17 00:00:00 2001 From: Andrey Fedorov Date: Tue, 4 Mar 2025 09:14:41 +0100 Subject: [PATCH] :bug: Fix relative position application for flex children --- .../app/main/data/workspace/transforms.cljs | 28 +++++++++++++------ .../app/main/ui/workspace/tokens/changes.cljs | 27 +++++++++++++++--- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/frontend/src/app/main/data/workspace/transforms.cljs b/frontend/src/app/main/data/workspace/transforms.cljs index 3d990c096..c62fa658d 100644 --- a/frontend/src/app/main/data/workspace/transforms.cljs +++ b/frontend/src/app/main/data/workspace/transforms.cljs @@ -859,12 +859,26 @@ (rx/of (reorder-selected-layout-child direction)) (rx/of (nudge-selected-shapes direction shift?))))))) +(defn- get-delta [position bbox] + (let [cpos (gpt/point (:x bbox) (:y bbox)) + pos (gpt/point (or (:x position) (:x bbox)) + (or (:y position) (:y bbox)))] + (gpt/subtract pos cpos))) + +(defn- get-relative-delta [position bbox parent] + (let [parent-bbox (-> parent :points grc/points->rect) + relative-cpos (gpt/subtract (gpt/point (:x bbox) (:y bbox)) + (gpt/point (:x parent-bbox) (:y parent-bbox))) + cpos (gpt/point (:x relative-cpos) (:y relative-cpos)) + pos (gpt/point (or (:x position) (:x relative-cpos)) + (or (:y position) (:y relative-cpos)))] + (gpt/subtract pos cpos))) + (defn update-position "Move shapes to a new position" ([id position] (update-position id position nil)) ([id position options] (dm/assert! (uuid? id)) - (ptk/reify ::update-position ptk/WatchEvent (watch [_ state _] @@ -874,15 +888,11 @@ shape (get objects id) ;; FIXME: performance rect bbox (-> shape :points grc/points->rect) - - cpos (gpt/point (:x bbox) (:y bbox)) - pos (gpt/point (or (:x position) (:x bbox)) - (or (:y position) (:y bbox))) - - delta (gpt/subtract pos cpos) - + parent (cfh/get-parent objects id) + delta (if-not (:relative? options) + (get-delta position bbox) + (get-relative-delta position bbox parent)) modif-tree (dwm/create-modif-tree [id] (ctm/move-modifiers delta))] - (rx/of (dwm/apply-modifiers {:modifiers modif-tree :page-id page-id :ignore-constraints false diff --git a/frontend/src/app/main/ui/workspace/tokens/changes.cljs b/frontend/src/app/main/ui/workspace/tokens/changes.cljs index dda0de6d4..87fc449af 100644 --- a/frontend/src/app/main/ui/workspace/tokens/changes.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/changes.cljs @@ -290,15 +290,35 @@ {:ignore-touched true :page-id page-id})))))))) +(defn- immediate-flex-absolute-child? [objects id] + (let [shape (get objects id)] + (boolean + (and (:layout-item-absolute shape) + (ctsl/flex-layout-immediate-child? objects shape))))) + (defn update-shape-position ([value shape-ids attributes] (update-shape-position value shape-ids attributes nil)) ([value shape-ids attributes page-id] (ptk/reify ::update-shape-position ptk/WatchEvent - (watch [_ _ _] + (watch [_ state _] (when (number? value) - (rx/concat - (map #(dwt/update-position % (zipmap attributes (repeat value)) {:ignore-touched true :page-id page-id}) shape-ids))))))) + (let [page-id' (or page-id (get state :current-page-id)) + objects (dsh/lookup-page-objects state page-id') + {canvas-children-ids false + flex-children-ids true} (group-by #(immediate-flex-absolute-child? objects %) shape-ids)] + (rx/concat + (concat + (map #(dwt/update-position % (zipmap attributes (repeat value)) + {:ignore-touched true + :page-id page-id'}) + canvas-children-ids) + (map #(dwt/update-position % (zipmap attributes (repeat value)) + {:ignore-touched true + :page-id page-id' + :relative? true}) + flex-children-ids))))))))) + (defn update-layout-sizing-limits ([value shape-ids attributes] (update-layout-sizing-limits value shape-ids attributes nil)) @@ -399,4 +419,3 @@ (defn token-attributes [token-type] (dm/get-in token-properties [token-type :attributes])) -