Skip to content

Commit

Permalink
Merge pull request #2868 from penpot/alotor-fix-layout-problems
Browse files Browse the repository at this point in the history
Fix layout problems
  • Loading branch information
superalex committed Jan 31, 2023
2 parents 2c5f35e + 3fe5cd3 commit d31138d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
12 changes: 6 additions & 6 deletions common/src/app/common/geom/shapes/intersect.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,10 @@
c2 (+ (* a2 (:x c)) (* b2 (:y c)))

;; Cramer's rule
det (- (* a1 b2) (* a2 b1))]
det (- (* a1 b2) (* a2 b1))
det (if (mth/almost-zero? det) 0.001 det)

;; If almost zero the lines are parallel
(when (not (mth/almost-zero? det))
(let [x (/ (- (* b2 c1) (* b1 c2)) det)
y (/ (- (* c2 a1) (* c1 a2)) det)]
(gpt/point x y)))))
x (/ (- (* b2 c1) (* b1 c2)) det)
y (/ (- (* c2 a1) (* c1 a2)) det)]

(gpt/point x y)))
17 changes: 10 additions & 7 deletions frontend/src/app/main/data/workspace/shape_layout.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,25 @@
{:layout :grid})

(defn get-layout-initializer
[type]
[type from-frame?]
(let [initial-layout-data (if (= type :flex) initial-flex-layout initial-grid-layout)]
(fn [shape]
(-> shape
(merge shape initial-layout-data)))))
(merge initial-layout-data)
;; If the original shape is not a frame we set clip content and show-viewer to false
(cond-> (not from-frame?)
(assoc :show-content true :hide-in-viewer true))))))

(defn create-layout-from-id
[ids type]
[ids type from-frame?]
(ptk/reify ::create-layout-from-id
ptk/WatchEvent
(watch [_ state _]
(let [objects (wsh/lookup-page-objects state)
children-ids (into [] (mapcat #(get-in objects [% :shapes])) ids)
undo-id (js/Symbol)]
(rx/of (dwu/start-undo-transaction undo-id)
(dwc/update-shapes ids (get-layout-initializer type))
(dwc/update-shapes ids (get-layout-initializer type from-frame?))
(ptk/data-event :layout/update ids)
(dwc/update-shapes children-ids #(dissoc % :constraints-h :constraints-v))
(dwu/commit-undo-transaction undo-id))))))
Expand Down Expand Up @@ -173,7 +176,7 @@
(dws/create-artboard-from-selection new-shape-id parent-id group-index)
(cl/remove-all-fills [new-shape-id] {:color clr/black
:opacity 1})
(create-layout-from-id [new-shape-id] type)
(create-layout-from-id [new-shape-id] type false)
(dwc/update-shapes
[new-shape-id]
(fn [shape]
Expand All @@ -193,7 +196,7 @@
(dws/create-artboard-from-selection new-shape-id)
(cl/remove-all-fills [new-shape-id] {:color clr/black
:opacity 1})
(create-layout-from-id [new-shape-id] type)
(create-layout-from-id [new-shape-id] type false)
(dwc/update-shapes
[new-shape-id]
(fn [shape]
Expand Down Expand Up @@ -233,7 +236,7 @@
(if (and single? is-frame?)
(rx/of
(dwu/start-undo-transaction undo-id)
(create-layout-from-id [(first selected)] :flex)
(create-layout-from-id [(first selected)] :flex true)
(dwu/commit-undo-transaction undo-id))
(rx/of
(dwu/start-undo-transaction undo-id)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/main/ui/workspace/context_menu.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@
is-flex-container? (and is-frame? (= :flex (:layout (first shapes))))
ids (->> shapes (map :id))
add-flex #(st/emit! (if is-frame?
(dwsl/create-layout-from-id ids :flex)
(dwsl/create-layout-from-id ids :flex true)
(dwsl/create-layout-from-selection :flex)))
remove-flex #(st/emit! (dwsl/remove-layout ids))]

Expand Down

0 comments on commit d31138d

Please sign in to comment.