Add to plugins clone and remove

This commit is contained in:
alonso.torres 2024-04-30 11:34:50 +02:00
parent e30c21a71f
commit 9243ba937d
4 changed files with 112 additions and 71 deletions

View file

@ -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]))
@ -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)

View file

@ -8,12 +8,11 @@
"RPC for plugins runtime."
(:require
[app.common.data.macros :as dm]
[app.common.spec :as us]
[app.common.uuid :as uuid]
[app.util.object :as obj]
[cuerdas.core :as str]))
(def uuid-regex
#"\w{8}-\w{4}-\w{4}-\w{4}-\w{12}")
[cuerdas.core :as str]
[promesa.core :as p]))
(defn get-data
([self attr]
@ -42,7 +41,7 @@
(let [v (cond (map? v)
(from-js v)
(and (string? v) (re-matches uuid-regex v))
(and (string? v) (re-matches us/uuid-rx v))
(uuid/uuid v)
:else v)]
@ -50,7 +49,6 @@
{}
ret)))
(defn to-js
"Converts to javascript an camelize the keys"
[obj]
@ -65,5 +63,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]))

View file

@ -9,6 +9,7 @@
(:require
[app.common.data.macros :as dm]
[app.common.record :as crc]
[app.common.spec :as us]
[app.common.uuid :as uuid]
[app.main.data.workspace.viewport :as dwv]
[app.main.data.workspace.zoom :as dwz]
@ -46,16 +47,17 @@
:set
(fn [_ value]
(let [new-x (obj/get value "x")
new-y (obj/get value "y")
vb (dm/get-in @st/state [:workspace-local :vbox])
old-x (+ (:x vb) (/ (:width vb) 2))
old-y (+ (:y vb) (/ (:height vb) 2))
delta-x (- new-x old-x)
delta-y (- new-y old-y)
to-position
{:x #(+ % delta-x)
:y #(+ % delta-y)}]
(st/emit! (dwv/update-viewport-position to-position))))}
new-y (obj/get value "y")]
(when (and (us/safe-number? new-x) (us/safe-number? new-y))
(let [vb (dm/get-in @st/state [:workspace-local :vbox])
old-x (+ (:x vb) (/ (:width vb) 2))
old-y (+ (:y vb) (/ (:height vb) 2))
delta-x (- new-x old-x)
delta-y (- new-y old-y)
to-position
{:x #(+ % delta-x)
:y #(+ % delta-y)}]
(st/emit! (dwv/update-viewport-position to-position))))))}
{:name "zoom"
:get
@ -63,8 +65,9 @@
(dm/get-in @st/state [:workspace-local :zoom]))
:set
(fn [_ value]
(let [z (dm/get-in @st/state [:workspace-local :zoom])]
(st/emit! (dwz/set-zoom (/ value z)))))}
(when (us/safe-number? value)
(let [z (dm/get-in @st/state [:workspace-local :zoom])]
(st/emit! (dwz/set-zoom (/ value z))))))}
{:name "bounds"
:get