0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-24 15:56:11 -05:00

Add api methods to align, distribute and flatten shapes

This commit is contained in:
alonso.torres 2024-09-12 16:12:30 +02:00
parent fb39dd5440
commit 8c1fba5160
3 changed files with 117 additions and 70 deletions

View file

@ -970,25 +970,27 @@
(map #(gal/align-to-rect % rect axis) selected-objs))) (map #(gal/align-to-rect % rect axis) selected-objs)))
(defn align-objects (defn align-objects
[axis] ([axis]
(dm/assert! (align-objects axis nil))
"expected valid align axis value" ([axis selected]
(contains? gal/valid-align-axis axis)) (dm/assert!
"expected valid align axis value"
(contains? gal/valid-align-axis axis))
(ptk/reify ::align-objects (ptk/reify ::align-objects
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ state _]
(let [objects (wsh/lookup-page-objects state) (let [objects (wsh/lookup-page-objects state)
selected (wsh/lookup-selected state) selected (or selected (wsh/lookup-selected state))
moved (if (= 1 (count selected)) moved (if (= 1 (count selected))
(align-object-to-parent objects (first selected) axis) (align-object-to-parent objects (first selected) axis)
(align-objects-list objects selected axis)) (align-objects-list objects selected axis))
undo-id (js/Symbol)] undo-id (js/Symbol)]
(when (can-align? selected objects) (when (can-align? selected objects)
(rx/of (dwu/start-undo-transaction undo-id) (rx/of (dwu/start-undo-transaction undo-id)
(dwt/position-shapes moved) (dwt/position-shapes moved)
(ptk/data-event :layout/update {:ids selected}) (ptk/data-event :layout/update {:ids selected})
(dwu/commit-undo-transaction undo-id))))))) (dwu/commit-undo-transaction undo-id))))))))
(defn can-distribute? [selected] (defn can-distribute? [selected]
(cond (cond
@ -997,25 +999,27 @@
:else true)) :else true))
(defn distribute-objects (defn distribute-objects
[axis] ([axis]
(dm/assert! (distribute-objects axis nil))
"expected valid distribute axis value" ([axis ids]
(contains? gal/valid-dist-axis axis)) (dm/assert!
"expected valid distribute axis value"
(contains? gal/valid-dist-axis axis))
(ptk/reify ::distribute-objects (ptk/reify ::distribute-objects
ptk/WatchEvent ptk/WatchEvent
(watch [_ state _] (watch [_ state _]
(let [page-id (:current-page-id state) (let [page-id (:current-page-id state)
objects (wsh/lookup-page-objects state page-id) objects (wsh/lookup-page-objects state page-id)
selected (wsh/lookup-selected state) selected (or ids (wsh/lookup-selected state))
moved (-> (map #(get objects %) selected) moved (-> (map #(get objects %) selected)
(gal/distribute-space axis)) (gal/distribute-space axis))
undo-id (js/Symbol)] undo-id (js/Symbol)]
(when (can-distribute? selected) (when (can-distribute? selected)
(rx/of (dwu/start-undo-transaction undo-id) (rx/of (dwu/start-undo-transaction undo-id)
(dwt/position-shapes moved) (dwt/position-shapes moved)
(ptk/data-event :layout/update {:ids selected}) (ptk/data-event :layout/update {:ids selected})
(dwu/commit-undo-transaction undo-id))))))) (dwu/commit-undo-transaction undo-id))))))))
;; --- Shape Proportions ;; --- Shape Proportions

View file

@ -15,24 +15,27 @@
[beicon.v2.core :as rx] [beicon.v2.core :as rx]
[potok.v2.core :as ptk])) [potok.v2.core :as ptk]))
(defn convert-selected-to-path [] (defn convert-selected-to-path
(ptk/reify ::convert-selected-to-path ([]
ptk/WatchEvent (convert-selected-to-path nil))
(watch [it state _] ([ids]
(let [page-id (:current-page-id state) (ptk/reify ::convert-selected-to-path
objects (wsh/lookup-page-objects state) ptk/WatchEvent
selected (->> (wsh/lookup-selected state) (watch [it state _]
(remove #(ctn/has-any-copy-parent? objects (get objects %)))) (let [page-id (:current-page-id state)
objects (wsh/lookup-page-objects state)
selected (->> (or ids (wsh/lookup-selected state))
(remove #(ctn/has-any-copy-parent? objects (get objects %))))
children-ids children-ids
(into #{} (into #{}
(mapcat #(cph/get-children-ids objects %)) (mapcat #(cph/get-children-ids objects %))
selected) selected)
changes changes
(-> (pcb/empty-changes it page-id) (-> (pcb/empty-changes it page-id)
(pcb/with-objects objects) (pcb/with-objects objects)
(pcb/update-shapes selected #(upsp/convert-to-path % objects)) (pcb/update-shapes selected #(upsp/convert-to-path % objects))
(pcb/remove-objects children-ids))] (pcb/remove-objects children-ids))]
(rx/of (dch/commit-changes changes)))))) (rx/of (dch/commit-changes changes)))))))

View file

@ -371,30 +371,70 @@
(st/emit! (dw/go-to-page id)))) (st/emit! (dw/go-to-page id))))
(alignHorizontal (alignHorizontal
[_ _shapes _direction] [_ shapes direction]
;; TODO (let [dir (case direction
) "left" :hleft
"center" :hcenter
"right" :hright
nil)]
(cond
(nil? dir)
(u/display-not-valid :alignHorizontal-direction "Direction not valid")
(or (not (array? shapes)) (not (every? shape/shape-proxy? shapes)))
(u/display-not-valid :alignHorizontal-shapes "Not valid shapes")
:else
(let [ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dw/align-objects dir ids))))))
(alignVertical (alignVertical
[_ _shapes _direction] [_ shapes direction]
;; TODO (let [dir (case direction
) "top" :vtop
"center" :vcenter
"bottom" :vbottom
nil)]
(cond
(nil? dir)
(u/display-not-valid :alignVertical-direction "Direction not valid")
(or (not (array? shapes)) (not (every? shape/shape-proxy? shapes)))
(u/display-not-valid :alignVertical-shapes "Not valid shapes")
:else
(let [ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dw/align-objects dir ids))))))
(distributeHorizontal (distributeHorizontal
[_ _shapes] [_ shapes]
;; TODO (cond
) (or (not (array? shapes)) (not (every? shape/shape-proxy? shapes)))
(u/display-not-valid :distributeHorizontal-shapes "Not valid shapes")
:else
(let [ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dw/distribute-objects :horizontal ids)))))
(distributeVertical (distributeVertical
[_ _shapes] [_ shapes]
;; TODO (cond
) (or (not (array? shapes)) (not (every? shape/shape-proxy? shapes)))
(u/display-not-valid :distributeVertical-shapes "Not valid shapes")
:else
(let [ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dw/distribute-objects :vertical ids)))))
(flatten (flatten
[_ _shapes] [_ shapes]
;; TODO (cond
) (or (not (array? shapes)) (not (every? shape/shape-proxy? shapes)))
) (u/display-not-valid :flatten-shapes "Not valid shapes")
:else
(let [ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dw/convert-selected-to-path ids))))))
(defn create-context (defn create-context
[plugin-id] [plugin-id]