Skip to content

Commit

Permalink
✨ Add to plugins clone and remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Alotor committed Apr 30, 2024
1 parent 3a741ef commit 5cee72d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 55 deletions.
112 changes: 63 additions & 49 deletions frontend/src/app/main/data/workspace/selection.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -723,62 +723,76 @@

(gpt/subtract new-pos pt-obj)))))

(defn duplicate-shapes
[ids & {:keys [move-delta? alt-duplication? change-selection? return-ref]
:or {move-delta? false alt-duplication? false change-selection? true return-ref nil}}]
(ptk/reify ::duplicate-shapes
ptk/WatchEvent
(watch [it state _]
(let [page (wsh/lookup-page state)
objects (:objects page)
ids (into #{}
(comp (map (d/getf objects))
(filter #(ctk/allow-duplicate? objects %))
(map :id))
ids)]
(when (seq ids)
(let [obj (get objects (first ids))
delta (if move-delta?
(calc-duplicate-delta obj state objects)
(gpt/point 0 0))

file-id (:current-file-id state)
libraries (wsh/get-libraries state)
library-data (wsh/get-file state file-id)

changes (->> (prepare-duplicate-changes objects page ids delta it libraries library-data file-id)
(duplicate-changes-update-indices objects ids))

tags (or (:tags changes) #{})

changes (cond-> changes alt-duplication? (assoc :tags (conj tags :alt-duplication)))

id-original (first ids)

new-ids (->> changes
:redo-changes
(filter #(= (:type %) :add-obj))
(filter #(ids (:old-id %)))
(map #(get-in % [:obj :id]))
(into (d/ordered-set)))

id-duplicated (first new-ids)

frames (into #{}
(map #(get-in objects [% :frame-id]))
ids)
undo-id (js/Symbol)]

;; Warning: This order is important for the focus mode.
(->> (rx/of
(dwu/start-undo-transaction undo-id)
(dch/commit-changes changes)
(when change-selection?
(select-shapes new-ids))
(ptk/data-event :layout/update {:ids frames})
(memorize-duplicated id-original id-duplicated)
(dwu/commit-undo-transaction undo-id))
(rx/tap #(when (some? return-ref)
(reset! return-ref id-duplicated))))))))))

(defn duplicate-selected
([move-delta?]
(duplicate-selected move-delta? false))
([move-delta? alt-duplication?]
(ptk/reify ::duplicate-selected
ptk/WatchEvent
(watch [it state _]
(watch [_ state _]
(when (or (not move-delta?) (nil? (get-in state [:workspace-local :transform])))
(let [page (wsh/lookup-page state)
objects (:objects page)
selected (->> (wsh/lookup-selected state)
(map (d/getf objects))
(filter #(ctk/allow-duplicate? objects %))
(map :id)
set)]
(when (seq selected)
(let [obj (get objects (first selected))
delta (if move-delta?
(calc-duplicate-delta obj state objects)
(gpt/point 0 0))

file-id (:current-file-id state)
libraries (wsh/get-libraries state)
library-data (wsh/get-file state file-id)

changes (->> (prepare-duplicate-changes objects page selected delta it libraries library-data file-id)
(duplicate-changes-update-indices objects selected))

tags (or (:tags changes) #{})

changes (cond-> changes alt-duplication? (assoc :tags (conj tags :alt-duplication)))

id-original (first selected)

new-selected (->> changes
:redo-changes
(filter #(= (:type %) :add-obj))
(filter #(selected (:old-id %)))
(map #(get-in % [:obj :id]))
(into (d/ordered-set)))

id-duplicated (first new-selected)

frames (into #{}
(map #(get-in objects [% :frame-id]))
selected)
undo-id (js/Symbol)]

;; Warning: This order is important for the focus mode.
(rx/of
(dwu/start-undo-transaction undo-id)
(dch/commit-changes changes)
(select-shapes new-selected)
(ptk/data-event :layout/update {:ids frames})
(memorize-duplicated id-original id-duplicated)
(dwu/commit-undo-transaction undo-id))))))))))
(let [selected (wsh/lookup-selected state)]
(rx/of (duplicate-shapes selected
:move-delta? move-delta?
:alt-duplication? alt-duplication?))))))))

(defn change-hover-state
[id value]
Expand Down
16 changes: 14 additions & 2 deletions frontend/src/app/plugins/shape.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
[app.common.uuid :as uuid]
[app.main.data.workspace :as udw]
[app.main.data.workspace.changes :as dwc]
[app.main.data.workspace.selection :as dws]
[app.main.data.workspace.shapes :as dwsh]
[app.main.store :as st]
[app.plugins.utils :as utils :refer [get-data get-data-fn]]
[app.util.object :as obj]))
Expand Down Expand Up @@ -61,8 +63,18 @@
(st/emit! (udw/update-dimensions [id] :width width)
(udw/update-dimensions [id] :height height))))

(clone [_] (.log js/console (clj->js _data)))
(delete [_] (.log js/console (clj->js _data)))
(clone [self]
(let [id (get-data self :id)
page-id (:current-page-id @st/state)
ret-v (atom nil)]
(st/emit! (dws/duplicate-shapes #{id} :change-selection? false :return-ref ret-v))
(let [new-id (deref ret-v)
shape (dm/get-in @st/state [:workspace-data :pages-index page-id :objects new-id])]
(data->shape-proxy shape))))

(remove [self]
(let [id (get-data self :id)]
(st/emit! (dwsh/delete-shapes #{id}))))

(appendChild [self child]
(let [parent-id (get-data self :id)
Expand Down
22 changes: 18 additions & 4 deletions frontend/src/app/plugins/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
[app.common.data.macros :as dm]
[app.common.uuid :as uuid]
[app.util.object :as obj]
[cuerdas.core :as str]))
[cuerdas.core :as str]
[promesa.core :as p]))

(def uuid-regex
#"\w{8}-\w{4}-\w{4}-\w{4}-\w{12}")
Expand Down Expand Up @@ -50,7 +51,6 @@
{}
ret)))


(defn to-js
"Converts to javascript an camelize the keys"
[obj]
Expand All @@ -65,5 +65,19 @@
obj)]
(clj->js result)))



(defn result-p
"Creates a pair of atom+promise. The promise will be resolved when the atom gets a value.
We use this to return the promise to the library clients and resolve its value when a value is passed
to the atom"
[]
(let [ret-v (atom nil)
ret-p
(p/create
(fn [resolve _]
(add-watch
ret-v
::watcher
(fn [_ _ _ value]
(remove-watch ret-v ::watcher)
(resolve value)))))]
[ret-v ret-p]))

0 comments on commit 5cee72d

Please sign in to comment.