mirror of
https://github.com/penpot/penpot.git
synced 2025-06-02 14:31:38 +02:00
✨ Add write apis to the plugins
This commit is contained in:
parent
c85f76300a
commit
00de89197e
4 changed files with 160 additions and 29 deletions
|
@ -7,8 +7,12 @@
|
|||
(ns app.plugins.shape
|
||||
"RPC for plugins runtime."
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.data.macros :as dm]
|
||||
[app.common.files.helpers :as cfh]
|
||||
[app.common.record :as crc]
|
||||
[app.common.text :as txt]
|
||||
[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]]
|
||||
|
@ -18,24 +22,51 @@
|
|||
|
||||
(defn- make-fills
|
||||
[fills]
|
||||
;; TODO: Transform explicitly?
|
||||
(apply array
|
||||
(->> fills
|
||||
(map #(clj->js % {:keyword-fn (fn [k] (str/camel (name k)))})))))
|
||||
(.freeze
|
||||
js/Object
|
||||
(apply array
|
||||
(->> fills
|
||||
;; TODO: Transform explicitly instead of cljs->js?
|
||||
(map #(clj->js % {:keyword-fn (fn [k] (str/camel (name k)))}))))))
|
||||
|
||||
(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)))}))))))
|
||||
|
||||
(defn- locate-shape
|
||||
[shape-id]
|
||||
(let [page-id (:current-page-id @st/state)]
|
||||
(dm/get-in @st/state [:workspace-data :pages-index page-id :objects shape-id])))
|
||||
|
||||
(deftype ShapeProxy [#_:clj-kondo/ignore _data]
|
||||
(defn- get-state
|
||||
([self attr]
|
||||
(let [id (get-data self :id)
|
||||
page-id (d/nilv (get-data self :page-id) (:current-page-id @st/state))]
|
||||
(dm/get-in @st/state [:workspace-data :pages-index page-id :objects id attr])))
|
||||
([self attr mapfn]
|
||||
(-> (get-state self attr)
|
||||
(mapfn))))
|
||||
|
||||
(deftype ShapeProxy [^:mutable #_:clj-kondo/ignore _data]
|
||||
Object
|
||||
(getChildren
|
||||
[self]
|
||||
(apply array (->> (get-data self :shapes)
|
||||
(apply array (->> (get-state self :shapes)
|
||||
(map locate-shape)
|
||||
(map data->shape-proxy))))
|
||||
|
||||
(resize
|
||||
[self width height]
|
||||
|
||||
(let [id (get-data self :id)]
|
||||
(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)))
|
||||
(appendChild [_] (.log js/console (clj->js _data))))
|
||||
|
@ -48,25 +79,61 @@
|
|||
(defn data->shape-proxy
|
||||
[data]
|
||||
|
||||
(crc/add-properties!
|
||||
(ShapeProxy. data)
|
||||
{:name "_data"
|
||||
:enumerable false}
|
||||
(-> (ShapeProxy. data)
|
||||
(crc/add-properties!
|
||||
{:name "_data"
|
||||
:enumerable false}
|
||||
|
||||
{:name "id"
|
||||
:get (get-data-fn :id str)}
|
||||
{:name "id"
|
||||
:get (get-data-fn :id str)}
|
||||
|
||||
{:name "name"
|
||||
:get (get-data-fn :name)
|
||||
:set (fn [self value]
|
||||
(let [id (get-data self :id)]
|
||||
(st/emit! (dwc/update-shapes [id] #(assoc % :name value)))))}
|
||||
{:name "type"
|
||||
:get (get-data-fn :type)}
|
||||
|
||||
{:name "children"
|
||||
:get #(.getChildren ^js %)}
|
||||
{:name "x"
|
||||
:get #(get-state % :x)
|
||||
:set
|
||||
(fn [self value]
|
||||
(let [id (get-data self :id)]
|
||||
(st/emit! (udw/update-position id {:x value}))))}
|
||||
|
||||
{:name "fills"
|
||||
:get (get-data-fn :fills make-fills)
|
||||
;;:set (fn [self value] (.log js/console self value))
|
||||
}))
|
||||
{:name "y"
|
||||
:get #(get-state % :y)
|
||||
:set
|
||||
(fn [self value]
|
||||
(let [id (get-data self :id)]
|
||||
(st/emit! (udw/update-position id {:y value}))))}
|
||||
|
||||
{:name "width"
|
||||
:get #(get-state % :width)}
|
||||
|
||||
{:name "height"
|
||||
:get #(get-state % :height)}
|
||||
|
||||
{:name "name"
|
||||
:get #(get-state % :name)
|
||||
:set (fn [self value]
|
||||
(let [id (get-data self :id)]
|
||||
(st/emit! (dwc/update-shapes [id] #(assoc % :name value)))))}
|
||||
|
||||
{:name "children"
|
||||
:get #(.getChildren ^js %)}
|
||||
|
||||
{:name "fills"
|
||||
:get #(get-state % :fills make-fills)
|
||||
;;:set (fn [self value] (.log js/console self value))
|
||||
}
|
||||
|
||||
{:name "strokes"
|
||||
:get #(get-state % :strokes make-strokes)
|
||||
;;:set (fn [self value] (.log js/console self value))
|
||||
})
|
||||
|
||||
(cond-> (cfh/text-shape? data)
|
||||
(crc/add-properties!
|
||||
{:name "characters"
|
||||
:get #(get-state % :content txt/content->text)
|
||||
:set (fn [self value]
|
||||
(let [id (get-data self :id)]
|
||||
(st/emit! (dwc/update-shapes [id] #(txt/change-text % value)))))}))))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue