Merge pull request #2589 from penpot/alotor-fixes

Flex layout fixes
This commit is contained in:
Andrey Antukh 2022-11-22 17:15:40 +01:00 committed by GitHub
commit c656dd146c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 134 additions and 52 deletions

View file

@ -141,7 +141,7 @@
[frame layout-data children] [frame layout-data children]
(let [reverse? (:reverse? layout-data) (let [reverse? (:reverse? layout-data)
children (vec (cond->> (d/enumerate children) reverse? reverse)) children (vec (cond->> (d/enumerate children) (not reverse?) reverse))
lines (:layout-lines layout-data)] lines (:layout-lines layout-data)]
(loop [areas [] (loop [areas []
@ -169,7 +169,7 @@
areas areas
(let [[child-area child-area-start child-area-end] (let [[child-area child-area-start child-area-end]
(drop-child-areas frame line-area child index reverse? prev-child-x prev-child-y (nil? (first children)))] (drop-child-areas frame line-area child index (not reverse?) prev-child-x prev-child-y (nil? (first children)))]
(recur (conj areas child-area-start child-area-end) (recur (conj areas child-area-start child-area-end)
(+ (:x child-area) (:width child-area)) (+ (:x child-area) (:width child-area))
(+ (:y child-area) (:height child-area)) (+ (:y child-area) (:height child-area))

View file

@ -304,7 +304,7 @@
(let [layout-bounds (layout-bounds shape) (let [layout-bounds (layout-bounds shape)
reverse? (ctl/reverse? shape) reverse? (ctl/reverse? shape)
children (cond->> children reverse? reverse) children (cond->> children (not reverse?) reverse)
;; Creates the layout lines information ;; Creates the layout lines information
layout-lines layout-lines

View file

@ -159,7 +159,7 @@
(let [children (map (d/getf objects) (:shapes parent)) (let [children (map (d/getf objects) (:shapes parent))
children (->> children (map (partial apply-modifiers modif-tree))) children (->> children (map (partial apply-modifiers modif-tree)))
layout-data (gcl/calc-layout-data parent children) layout-data (gcl/calc-layout-data parent children)
children (into [] (cond-> children (:reverse? layout-data) reverse)) children (into [] (cond-> children (not (:reverse? layout-data)) reverse))
max-idx (dec (count children)) max-idx (dec (count children))
layout-lines (:layout-lines layout-data)] layout-lines (:layout-lines layout-data)]

View file

@ -43,11 +43,12 @@
(defn move-position-data (defn move-position-data
[position-data dx dy] [position-data dx dy]
(when (some? position-data)
(cond->> position-data (cond->> position-data
(d/num? dx dy) (d/num? dx dy)
(mapv #(-> % (mapv #(-> %
(update :x + dx) (update :x + dx)
(update :y + dy))))) (update :y + dy))))))
(defn move (defn move
"Move the shape relatively to its current "Move the shape relatively to its current
@ -284,6 +285,8 @@
[shape transform-mtx] [shape transform-mtx]
(let [bool? (= (:type shape) :bool) (let [bool? (= (:type shape) :bool)
path? (= (:type shape) :path) path? (= (:type shape) :path)
text? (= (:type shape) :text)
{dx :x dy :y} (gpt/transform (gpt/point) transform-mtx)
points (gco/transform-points (:points shape) transform-mtx) points (gco/transform-points (:points shape) transform-mtx)
selrect (gco/transform-selrect (:selrect shape) transform-mtx)] selrect (gco/transform-selrect (:selrect shape) transform-mtx)]
(-> shape (-> shape
@ -291,6 +294,8 @@
(update :bool-content gpa/transform-content transform-mtx)) (update :bool-content gpa/transform-content transform-mtx))
(cond-> path? (cond-> path?
(update :content gpa/transform-content transform-mtx)) (update :content gpa/transform-content transform-mtx))
(cond-> text?
(update :position-data move-position-data dx dy))
(cond-> (not path?) (cond-> (not path?)
(assoc :x (:x selrect) (assoc :x (:x selrect)
:y (:y selrect) :y (:y selrect)

View file

@ -109,6 +109,11 @@
(recur (conj result parent-id) parent-id) (recur (conj result parent-id) parent-id)
result)))) result))))
(defn get-siblings-ids
[objects id]
(let [parent (get-parent objects id)]
(into [] (->> (:shapes parent) (remove #(= % id))))))
(defn get-frame (defn get-frame
"Get the frame that contains the shape. If the shape is already a "Get the frame that contains the shape. If the shape is already a
frame, get itself. If no shape is provided, returns the root frame." frame, get itself. If no shape is provided, returns the root frame."

View file

@ -1553,7 +1553,8 @@
(into (d/ordered-set)))] (into (d/ordered-set)))]
(rx/of (dch/commit-changes changes) (rx/of (dch/commit-changes changes)
(dws/select-shapes selected))))] (dws/select-shapes selected)
(dwul/update-layout-positions [frame-id]))))]
(ptk/reify ::paste-shape (ptk/reify ::paste-shape
ptk/WatchEvent ptk/WatchEvent

View file

@ -15,6 +15,7 @@
[app.common.pages.helpers :as cph] [app.common.pages.helpers :as cph]
[app.common.spec :as us] [app.common.spec :as us]
[app.common.types.modifiers :as ctm] [app.common.types.modifiers :as ctm]
[app.common.types.shape :as cts]
[app.common.types.shape.layout :as ctl] [app.common.types.shape.layout :as ctl]
[app.main.data.workspace.changes :as dch] [app.main.data.workspace.changes :as dch]
[app.main.data.workspace.comments :as-alias dwcm] [app.main.data.workspace.comments :as-alias dwcm]
@ -188,6 +189,42 @@
[(get-in objects [k :name]) v])) [(get-in objects [k :name]) v]))
modif-tree))) modif-tree)))
(defn apply-text-modifiers
[objects text-modifiers]
(letfn [(apply-text-modifier
[shape {:keys [width height]}]
(cond-> shape
(some? width)
(assoc :width width)
(some? height)
(assoc :height height)
(or (some? width) (some? height))
(cts/setup-rect-selrect)))]
(loop [modifiers (seq text-modifiers)
result objects]
(if (empty? modifiers)
result
(let [[id text-modifier] (first modifiers)]
(recur (rest modifiers)
(update objects id apply-text-modifier text-modifier)))))))
#_(defn apply-path-modifiers
[objects path-modifiers]
(letfn [(apply-path-modifier
[shape {:keys [content-modifiers]}]
(let [shape (update shape :content upc/apply-content-modifiers content-modifiers)
[points selrect] (helpers/content->points+selrect shape (:content shape))]
(assoc shape :selrect selrect :points points)))]
(loop [modifiers (seq path-modifiers)
result objects]
(if (empty? modifiers)
result
(let [[id path-modifier] (first modifiers)]
(recur (rest modifiers)
(update objects id apply-path-modifier path-modifier)))))))
(defn set-modifiers (defn set-modifiers
([modif-tree] ([modif-tree]
(set-modifiers modif-tree false)) (set-modifiers modif-tree false))
@ -206,7 +243,10 @@
(and (not ignore-snap-pixel) (contains? (:workspace-layout state) :snap-pixel-grid)) (and (not ignore-snap-pixel) (contains? (:workspace-layout state) :snap-pixel-grid))
modif-tree modif-tree
(gsh/set-objects-modifiers modif-tree objects ignore-constraints snap-pixel?)] (as-> objects $
(apply-text-modifiers $ (get state :workspace-text-modifier))
;;(apply-path-modifiers $ (get-in state [:workspace-local :edit-path]))
(gsh/set-objects-modifiers modif-tree $ ignore-constraints snap-pixel?))]
(assoc state :workspace-modifiers modif-tree)))))) (assoc state :workspace-modifiers modif-tree))))))

View file

@ -21,6 +21,7 @@
[app.main.data.workspace.path.state :as st] [app.main.data.workspace.path.state :as st]
[app.main.data.workspace.path.streams :as streams] [app.main.data.workspace.path.streams :as streams]
[app.main.data.workspace.path.undo :as undo] [app.main.data.workspace.path.undo :as undo]
[app.main.data.workspace.shapes-update-layout :as dwul]
[app.main.data.workspace.state-helpers :as wsh] [app.main.data.workspace.state-helpers :as wsh]
[app.main.streams :as ms] [app.main.streams :as ms]
[app.util.path.tools :as upt] [app.util.path.tools :as upt]
@ -297,22 +298,25 @@
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [mode (get-in state [:workspace-local :edit-path id :edit-mode])] (let [mode (get-in state [:workspace-local :edit-path id :edit-mode])
stopper (->> stream (rx/filter (ptk/type? ::start-path-edit)))
interrupt (->> stream (rx/filter #(= % :interrupt)) (rx/take 1))]
(rx/concat (rx/concat
(rx/of (undo/start-path-undo)) (rx/of (undo/start-path-undo))
(rx/of (drawing/change-edit-mode mode)) (rx/of (drawing/change-edit-mode mode))
(->> stream (->> interrupt
(rx/take-until (->> stream (rx/filter (ptk/type? ::start-path-edit)))) (rx/map #(stop-path-edit id))
(rx/filter #(= % :interrupt)) (rx/take-until stopper)))))))
(rx/take 1)
(rx/map #(stop-path-edit))))))))
(defn stop-path-edit [] (defn stop-path-edit [id]
(ptk/reify ::stop-path-edit (ptk/reify ::stop-path-edit
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [id (get-in state [:workspace-local :edition])] (update state :workspace-local dissoc :edit-path id))
(update state :workspace-local dissoc :edit-path id)))))
ptk/WatchEvent
(watch [_ _ _]
(rx/of (dwul/update-layout-positions [id])))))
(defn split-segments (defn split-segments
[{:keys [from-p to-p t]}] [{:keys [from-p to-p t]}]

View file

@ -8,9 +8,7 @@
(:require (:require
[app.common.data :as d] [app.common.data :as d]
[app.common.types.modifiers :as ctm] [app.common.types.modifiers :as ctm]
[app.common.types.shape.layout :as ctl]
[app.main.data.workspace.modifiers :as dwm] [app.main.data.workspace.modifiers :as dwm]
[app.main.data.workspace.state-helpers :as wsh]
[beicon.core :as rx] [beicon.core :as rx]
[potok.core :as ptk])) [potok.core :as ptk]))
@ -18,11 +16,9 @@
[ids] [ids]
(ptk/reify ::update-layout-positions (ptk/reify ::update-layout-positions
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ _ _]
(let [objects (wsh/lookup-page-objects state)
ids (->> ids (filter (partial ctl/layout? objects)))]
(if (d/not-empty? ids) (if (d/not-empty? ids)
(let [modif-tree (dwm/create-modif-tree ids (ctm/reflow-modifiers))] (let [modif-tree (dwm/create-modif-tree ids (ctm/reflow-modifiers))]
(rx/of (dwm/set-modifiers modif-tree) (rx/of (dwm/set-modifiers modif-tree)
(dwm/apply-modifiers))) (dwm/apply-modifiers)))
(rx/empty)))))) (rx/empty)))))

View file

@ -14,11 +14,13 @@
[app.common.pages.helpers :as cph] [app.common.pages.helpers :as cph]
[app.common.text :as txt] [app.common.text :as txt]
[app.common.types.modifiers :as ctm] [app.common.types.modifiers :as ctm]
[app.common.types.shape :as cts]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.main.data.workspace.changes :as dch] [app.main.data.workspace.changes :as dch]
[app.main.data.workspace.common :as dwc] [app.main.data.workspace.common :as dwc]
[app.main.data.workspace.selection :as dws] [app.main.data.workspace.selection :as dws]
[app.main.data.workspace.shapes :as dwsh] [app.main.data.workspace.shapes :as dwsh]
[app.main.data.workspace.shapes-update-layout :as dwul]
[app.main.data.workspace.state-helpers :as wsh] [app.main.data.workspace.state-helpers :as wsh]
[app.main.data.workspace.undo :as dwu] [app.main.data.workspace.undo :as dwu]
[app.util.router :as rt] [app.util.router :as rt]
@ -75,7 +77,8 @@
(dch/update-shapes [id] (fn [shape] (dch/update-shapes [id] (fn [shape]
(-> shape (-> shape
(assoc :content content) (assoc :content content)
(merge modifiers)))) (merge modifiers)
(cts/setup-rect-selrect))))
(dwu/commit-undo-transaction))))) (dwu/commit-undo-transaction)))))
(when (some? id) (when (some? id)
@ -316,7 +319,8 @@
[id new-width new-height] [id new-width new-height]
(ptk/reify ::resize-text (ptk/reify ::resize-text
ptk/WatchEvent ptk/WatchEvent
(watch [_ _ _] (watch [_ state _]
(let [shape (wsh/lookup-shape state id)]
(letfn [(update-fn [shape] (letfn [(update-fn [shape]
(let [{:keys [selrect grow-type]} shape (let [{:keys [selrect grow-type]} shape
{shape-width :width shape-height :height} selrect] {shape-width :width shape-height :height} selrect]
@ -328,7 +332,11 @@
(or (= grow-type :auto-height) (= grow-type :auto-width))) (or (= grow-type :auto-height) (= grow-type :auto-width)))
(gsh/transform-shape (ctm/change-dimensions-modifiers shape :height new-height)))))] (gsh/transform-shape (ctm/change-dimensions-modifiers shape :height new-height)))))]
(rx/of (dch/update-shapes [id] update-fn {:reg-objects? true :save-undo? false})))))) (when (or (and (not-changed? (:width shape) new-width) (= (:grow-type shape) :auto-width))
(and (not-changed? (:height shape) new-height)
(or (= (:grow-type shape) :auto-height) (= (:grow-type shape) :auto-width))))
(rx/of (dch/update-shapes [id] update-fn {:reg-objects? true :save-undo? false})
(dwul/update-layout-positions [id]))))))))
(defn save-font (defn save-font
[data] [data]
@ -358,11 +366,9 @@
(gpt/subtract (gpt/point (:selrect new-shape)) (gpt/subtract (gpt/point (:selrect new-shape))
(gpt/point (:selrect shape))) (gpt/point (:selrect shape)))
new-shape new-shape
(update new-shape :position-data gsh/move-position-data (:x delta-move) (:y delta-move))] (update new-shape :position-data gsh/move-position-data (:x delta-move) (:y delta-move))]
new-shape)) new-shape))
(defn update-text-modifier (defn update-text-modifier
@ -370,7 +376,16 @@
(ptk/reify ::update-text-modifier (ptk/reify ::update-text-modifier
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(update-in state [:workspace-text-modifier id] (fnil merge {}) props)))) (update-in state [:workspace-text-modifier id] (fnil merge {}) props))
ptk/WatchEvent
(watch [_ state _]
(let [shape (wsh/lookup-shape state id)]
(when (or (and (some? (:width props))
(not (mth/close? (:width props) (:width shape))))
(and (some? (:height props))
(not (mth/close? (:height props) (:height shape)))))
(rx/of (dwul/update-layout-positions [id])))))))
(defn clean-text-modifier (defn clean-text-modifier
[id] [id]

View file

@ -435,10 +435,16 @@
zoom (get-in state [:workspace-local :zoom] 1) zoom (get-in state [:workspace-local :zoom] 1)
focus (:workspace-focus-selected state) focus (:workspace-focus-selected state)
exclude-frames (into #{} exclude-frames
(into #{}
(filter (partial cph/frame-shape? objects)) (filter (partial cph/frame-shape? objects))
(cph/selected-with-children objects selected)) (cph/selected-with-children objects selected))
exclude-frames-siblings
(into exclude-frames
(mapcat (partial cph/get-siblings-ids objects))
selected)
fix-axis fix-axis
(fn [[position shift?]] (fn [[position shift?]]
(let [delta (gpt/to-vec from-position position)] (let [delta (gpt/to-vec from-position position)]
@ -471,9 +477,12 @@
;; We try to use the previous snap so we don't have to wait for the result of the new ;; We try to use the previous snap so we don't have to wait for the result of the new
(rx/map snap/correct-snap-point) (rx/map snap/correct-snap-point)
(rx/with-latest vector ms/mouse-position-mod)
(rx/map (rx/map
(fn [move-vector] (fn [[move-vector mod?]]
(let [position (gpt/add from-position move-vector) (let [position (gpt/add from-position move-vector)
exclude-frames (if mod? exclude-frames exclude-frames-siblings)
target-frame (ctst/top-nested-frame objects position exclude-frames) target-frame (ctst/top-nested-frame objects position exclude-frames)
layout? (ctl/layout? objects target-frame) layout? (ctl/layout? objects target-frame)
drop-index (when layout? (gsl/get-drop-index target-frame objects position))] drop-index (when layout? (gsl/get-drop-index target-frame objects position))]

View file

@ -28,9 +28,12 @@
(let [selfn #(get-in % [:edit-path id])] (let [selfn #(get-in % [:edit-path id])]
#(l/derived selfn refs/workspace-local)))) #(l/derived selfn refs/workspace-local))))
(defn content-modifiers-ref
[id]
(l/derived #(get-in % [:edit-path id :content-modifiers]) refs/workspace-local))
(defn make-content-modifiers-ref [id] (defn make-content-modifiers-ref [id]
(mf/use-memo (mf/use-memo
(mf/deps id) (mf/deps id)
(let [selfn #(get-in % [:edit-path id :content-modifiers])] #(content-modifiers-ref id)))
#(l/derived selfn refs/workspace-local))))

View file

@ -155,7 +155,11 @@
(not (identical? old-shape new-shape)) (not (identical? old-shape new-shape))
(not= (dissoc old-shape :migrate) (not= (dissoc old-shape :migrate)
(dissoc new-shape :migrate))) (dissoc new-shape :migrate)))
(not= new-modifiers old-modifiers) (and (not= new-modifiers old-modifiers)
(or (nil? new-modifiers)
(nil? old-modifiers)
(not (ctm/only-move? new-modifiers))
(not (ctm/only-move? old-modifiers))))
;; When the position data is nil we force to recalculate ;; When the position data is nil we force to recalculate
(:migrate new-shape)))) (:migrate new-shape))))