Add to plugin api: upload media, group and ungroup

This commit is contained in:
alonso.torres 2024-04-29 12:20:15 +02:00
parent 75d8965365
commit 21d38a058b
6 changed files with 148 additions and 51 deletions

View file

@ -13,12 +13,18 @@
[app.common.types.shape :as cts]
[app.common.uuid :as uuid]
[app.main.data.workspace.changes :as ch]
[app.main.data.workspace.groups :as dwg]
[app.main.data.workspace.media :as dwm]
[app.main.store :as st]
[app.plugins.events :as events]
[app.plugins.file :as file]
[app.plugins.page :as page]
[app.plugins.shape :as shape]
[app.plugins.viewport :as viewport]))
[app.plugins.utils :as utils]
[app.plugins.viewport :as viewport]
[app.util.object :as obj]
[beicon.v2.core :as rx]
[promesa.core :as p]))
;;
;; PLUGINS PUBLIC API - The plugins will able to access this functions
@ -33,7 +39,7 @@
[type]
(let [page-id (:current-page-id @st/state)
page (dm/get-in @st/state [:workspace-data :pages-index page-id])
shape (cts/setup-shape {:type :type
shape (cts/setup-shape {:type type
:x 0 :y 0 :width 100 :height 100})
changes
(-> (cb/empty-changes)
@ -89,13 +95,39 @@
"dark"
(get-in @st/state [:profile :theme]))))
(uploadMediaUrl
[_ name url]
(let [file-id (get-in @st/state [:workspace-file :id])]
(p/create
(fn [resolve reject]
(->> (dwm/upload-media-url name file-id url)
(rx/map utils/to-js)
(rx/take 1)
(rx/subs! resolve reject))))))
(group
[_ shapes]
(let [page-id (:current-page-id @st/state)
id (uuid/next)
ids (into #{} (map #(get (obj/get % "_data") :id)) shapes)]
(st/emit! (dwg/group-shapes id ids))
(shape/data->shape-proxy
(dm/get-in @st/state [:workspace-data :pages-index page-id :objects id]))))
(ungroup
[_ group & rest]
(let [shapes (concat [group] rest)
ids (into #{} (map #(get (obj/get % "_data") :id)) shapes)]
(st/emit! (dwg/ungroup-shapes ids))))
(createFrame
[_]
(create-shape :frame))
(createRectangle
[_]
(create-shape :rect)))
(create-shape :rect))
)
(defn create-context
[]

View file

@ -16,9 +16,8 @@
[app.main.data.workspace :as udw]
[app.main.data.workspace.changes :as dwc]
[app.main.store :as st]
[app.plugins.utils :refer [get-data get-data-fn]]
[app.util.object :as obj]
[cuerdas.core :as str]))
[app.plugins.utils :as utils :refer [get-data get-data-fn]]
[app.util.object :as obj]))
(declare data->shape-proxy)
@ -26,19 +25,13 @@
[fills]
(.freeze
js/Object
(apply array
(->> fills
;; TODO: Transform explicitly instead of cljs->js?
(map #(clj->js % {:keyword-fn (fn [k] (str/camel (name k)))}))))))
(apply array (->> fills (map utils/to-js)))))
(defn- make-strokes
[strokes]
(.freeze
js/Object
(apply array
(->> strokes
;; TODO: Transform explicitly instead of cljs->js?
(map #(clj->js % {:keyword-fn (fn [k] (str/camel (name k)))}))))))
(apply array (->> strokes (map utils/to-js)))))
(defn- locate-shape
[shape-id]
@ -64,7 +57,6 @@
(resize
[self width height]
(let [id (get-data self :id)]
(st/emit! (udw/update-dimensions [id] :width width)
(udw/update-dimensions [id] :height height))))
@ -99,7 +91,7 @@
:get (get-data-fn :id str)}
{:name "type"
:get (get-data-fn :type)}
:get (get-data-fn :type name)}
{:name "x"
:get #(get-state % :x)
@ -129,12 +121,18 @@
{:name "fills"
:get #(get-state % :fills make-fills)
;;:set (fn [self value] (.log js/console self value))
:set (fn [self value]
(let [id (get-data self :id)
value (mapv #(utils/from-js %) value)]
(st/emit! (dwc/update-shapes [id] #(assoc % :fills value)))))
}
{:name "strokes"
:get #(get-state % :strokes make-strokes)
;;:set (fn [self value] (.log js/console self value))
:set (fn [self value]
(let [id (get-data self :id)
value (mapv #(utils/from-js %) value)]
(st/emit! (dwc/update-shapes [id] #(assoc % :strokes value)))))
})
(cond-> (or (cfh/frame-shape? data) (cfh/group-shape? data) (cfh/svg-raw-shape? data) (cfh/bool-shape? data))

View file

@ -7,7 +7,13 @@
(ns app.plugins.utils
"RPC for plugins runtime."
(:require
[app.util.object :as obj]))
[app.common.data.macros :as dm]
[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}")
(defn get-data
([self attr]
@ -27,4 +33,37 @@
(fn [self]
(get-data self attr transform-fn))))
(defn from-js
"Converts the object back to js"
[obj]
(let [ret (js->clj obj {:keyword-fn (fn [k] (str/camel (name k)))})]
(reduce-kv
(fn [m k v]
(let [v (cond (map? v)
(from-js v)
(and (string? v) (re-matches uuid-regex v))
(uuid/uuid v)
:else v)]
(assoc m (keyword (str/kebab k)) v)))
{}
ret)))
(defn to-js
"Converts to javascript an camelize the keys"
[obj]
(let [result
(reduce-kv
(fn [m k v]
(let [v (cond (object? v) (to-js v)
(uuid? v) (dm/str v)
:else v)]
(assoc m (str/camel (name k)) v)))
{}
obj)]
(clj->js result)))

View file

@ -8,15 +8,11 @@
"RPC for plugins runtime."
(:require
[app.common.data.macros :as dm]
[app.common.geom.point :as gpt]
[app.common.record :as crc]
[app.common.record :as crc]
[app.common.uuid :as uuid]
[app.main.data.workspace.viewport :as dwv]
[app.main.data.workspace.zoom :as dwz]
[app.main.store :as st]
[app.plugins.page :as page]
[app.plugins.utils :refer [get-data-fn]]
[app.util.object :as obj]))
(deftype ViewportProxy []