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,7 +970,9 @@
(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]
(align-objects axis nil))
([axis selected]
(dm/assert! (dm/assert!
"expected valid align axis value" "expected valid align axis value"
(contains? gal/valid-align-axis axis)) (contains? gal/valid-align-axis axis))
@ -979,7 +981,7 @@
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))
@ -988,7 +990,7 @@
(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,7 +999,9 @@
:else true)) :else true))
(defn distribute-objects (defn distribute-objects
[axis] ([axis]
(distribute-objects axis nil))
([axis ids]
(dm/assert! (dm/assert!
"expected valid distribute axis value" "expected valid distribute axis value"
(contains? gal/valid-dist-axis axis)) (contains? gal/valid-dist-axis axis))
@ -1007,7 +1011,7 @@
(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)]
@ -1015,7 +1019,7 @@
(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,13 +15,16 @@
[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
([]
(convert-selected-to-path nil))
([ids]
(ptk/reify ::convert-selected-to-path (ptk/reify ::convert-selected-to-path
ptk/WatchEvent ptk/WatchEvent
(watch [it state _] (watch [it state _]
(let [page-id (:current-page-id state) (let [page-id (:current-page-id state)
objects (wsh/lookup-page-objects state) objects (wsh/lookup-page-objects state)
selected (->> (wsh/lookup-selected state) selected (->> (or ids (wsh/lookup-selected state))
(remove #(ctn/has-any-copy-parent? objects (get objects %)))) (remove #(ctn/has-any-copy-parent? objects (get objects %))))
children-ids children-ids
@ -35,4 +38,4 @@
(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]