mirror of
https://github.com/penpot/penpot.git
synced 2025-01-27 00:49:28 -05:00
♻️ Refactor add-shape
event.
And remove `add-frame` because `add-shape` is now generic.
This commit is contained in:
parent
3eb7cb0bb1
commit
a591e4539c
2 changed files with 32 additions and 63 deletions
|
@ -953,21 +953,6 @@
|
||||||
(recur (inc counter))
|
(recur (inc counter))
|
||||||
candidate)))))
|
candidate)))))
|
||||||
|
|
||||||
(defn impl-assoc-shape
|
|
||||||
"Add a shape to the current workspace page, inside a given frame.
|
|
||||||
Give it a name that is unique in the page"
|
|
||||||
[state {:keys [id frame-id] :as data}]
|
|
||||||
(let [page-id (::page-id state)
|
|
||||||
objects (get-in state [:workspace-data page-id :objects])
|
|
||||||
name (impl-generate-unique-name objects (:name data))
|
|
||||||
shape (assoc data :name name)
|
|
||||||
page-id (::page-id state)]
|
|
||||||
(-> state
|
|
||||||
(update-in [:workspace-data page-id :objects frame-id :shapes] conj id)
|
|
||||||
(update-in [:workspace-data page-id :objects] assoc id shape))))
|
|
||||||
|
|
||||||
(declare select-shape)
|
|
||||||
|
|
||||||
(defn- calculate-frame-overlap
|
(defn- calculate-frame-overlap
|
||||||
[objects shape]
|
[objects shape]
|
||||||
(let [rshp (geom/shape->rect-shape shape)
|
(let [rshp (geom/shape->rect-shape shape)
|
||||||
|
@ -987,53 +972,40 @@
|
||||||
(defn add-shape
|
(defn add-shape
|
||||||
[attrs]
|
[attrs]
|
||||||
(us/verify ::shape-attrs attrs)
|
(us/verify ::shape-attrs attrs)
|
||||||
(let [id (uuid/next)]
|
(ptk/reify ::add-shape
|
||||||
(ptk/reify ::add-shape
|
ptk/WatchEvent
|
||||||
ptk/UpdateEvent
|
(watch [_ state stream]
|
||||||
(update [_ state]
|
(let [page-id (::page-id state)
|
||||||
(let [page-id (::page-id state)
|
objects (get-in state [:workspace-data page-id :objects])
|
||||||
objects (get-in state [:workspace-data page-id :objects])
|
|
||||||
shape (-> (geom/setup-proportions attrs)
|
|
||||||
(assoc :id id))
|
|
||||||
frame-id (calculate-frame-overlap objects shape)
|
|
||||||
shape (merge cp/default-shape-attrs shape {:frame-id frame-id})]
|
|
||||||
(-> state
|
|
||||||
(impl-assoc-shape shape)
|
|
||||||
(assoc-in [:workspace-local :selected] #{id}))))
|
|
||||||
|
|
||||||
ptk/WatchEvent
|
id (uuid/next)
|
||||||
(watch [_ state stream]
|
shape (geom/setup-proportions attrs)
|
||||||
(let [page-id (::page-id state)
|
|
||||||
obj (get-in state [:workspace-data page-id :objects id])]
|
|
||||||
(rx/of (commit-changes [{:type :add-obj
|
|
||||||
:id id
|
|
||||||
:frame-id (:frame-id obj)
|
|
||||||
:obj obj}]
|
|
||||||
[{:type :del-obj
|
|
||||||
:id id}])))))))
|
|
||||||
|
|
||||||
(defn add-frame
|
unames (retrieve-used-names objects)
|
||||||
[data]
|
name (generate-unique-name unames (:name shape))
|
||||||
(us/verify ::shape-attrs data)
|
|
||||||
(let [id (uuid/next)]
|
|
||||||
(ptk/reify ::add-frame
|
|
||||||
ptk/UpdateEvent
|
|
||||||
(update [_ state]
|
|
||||||
(let [shape (-> (geom/setup-proportions data)
|
|
||||||
(assoc :id id))
|
|
||||||
shape (merge cp/default-frame-attrs shape)]
|
|
||||||
(impl-assoc-shape state shape)))
|
|
||||||
|
|
||||||
ptk/WatchEvent
|
frame-id (if (= :frame (:type shape))
|
||||||
(watch [_ state stream]
|
uuid/zero
|
||||||
(let [page-id (::page-id state)
|
(calculate-frame-overlap objects shape))
|
||||||
obj (get-in state [:workspace-data page-id :objects id])]
|
|
||||||
(rx/of (commit-changes [{:type :add-obj
|
shape (merge
|
||||||
:id id
|
(if (= :frame (:type shape))
|
||||||
:frame-id (:frame-id obj)
|
cp/default-frame-attrs
|
||||||
:obj obj}]
|
cp/default-shape-attrs)
|
||||||
[{:type :del-obj
|
(assoc shape
|
||||||
:id id}])))))))
|
:id id
|
||||||
|
:name name
|
||||||
|
:frame-id frame-id))
|
||||||
|
|
||||||
|
rchange {:type :add-obj
|
||||||
|
:id id
|
||||||
|
:frame-id frame-id
|
||||||
|
:obj shape}
|
||||||
|
uchange {:type :del-obj
|
||||||
|
:id id}]
|
||||||
|
|
||||||
|
(rx/of (commit-changes [rchange] [uchange] {:commit-local? true})
|
||||||
|
(select-shapes #{id}))))))
|
||||||
|
|
||||||
|
|
||||||
;; --- Duplicate Shapes
|
;; --- Duplicate Shapes
|
||||||
|
|
|
@ -275,10 +275,7 @@
|
||||||
shape)
|
shape)
|
||||||
shape (dissoc shape ::initialized? :resize-modifier)]
|
shape (dissoc shape ::initialized? :resize-modifier)]
|
||||||
;; Add & select the created shape to the workspace
|
;; Add & select the created shape to the workspace
|
||||||
(rx/of dw/deselect-all
|
(rx/of dw/deselect-all (dw/add-shape shape)))))))))
|
||||||
(if (= :frame (:type shape))
|
|
||||||
(dw/add-frame shape)
|
|
||||||
(dw/add-shape shape))))))))))
|
|
||||||
|
|
||||||
(def close-drawing-path
|
(def close-drawing-path
|
||||||
(ptk/reify ::close-drawing-path
|
(ptk/reify ::close-drawing-path
|
||||||
|
|
Loading…
Add table
Reference in a new issue