mirror of
https://github.com/penpot/penpot.git
synced 2025-12-12 06:24:17 +01:00
✨ New apis for plugins
This commit is contained in:
committed by
Andrey Antukh
parent
d6de1fdbdf
commit
3ca5b13e27
@@ -7,71 +7,134 @@
|
||||
(ns app.plugins.library
|
||||
"RPC for plugins runtime."
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.data.macros :as dm]
|
||||
[app.common.record :as cr]
|
||||
[app.main.store :as st]
|
||||
[app.plugins.utils :as utils :refer [get-data]]
|
||||
[app.util.object :as obj]))
|
||||
[app.plugins.utils :as u]))
|
||||
|
||||
(defn get-library-info
|
||||
([self attr]
|
||||
(let [lib-id (get-data self :id)
|
||||
current-file-id (:current-file-id @st/state)]
|
||||
(if (= lib-id current-file-id)
|
||||
(dm/get-in @st/state [:workspace-file attr])
|
||||
(dm/get-in @st/state [:workspace-libraries lib-id attr]))))
|
||||
(deftype LibraryColorProxy [$file $id]
|
||||
Object
|
||||
|
||||
([self attr mapfn]
|
||||
(-> (get-library-info self attr)
|
||||
(mapfn))))
|
||||
(asFill [_]
|
||||
(let [color (u/locate-library-color $file $id)]
|
||||
(u/to-js
|
||||
(d/without-nils
|
||||
{:fill-color (:color color)
|
||||
:fill-opacity (:opacity color)
|
||||
:fill-color-gradient (:gradient color)
|
||||
:fill-color-ref-file $file
|
||||
:fill-color-ref-id $id
|
||||
:fill-image (:image color)}))))
|
||||
|
||||
(defn get-library-data
|
||||
([self attr]
|
||||
(let [lib-id (get-data self :id)
|
||||
current-file-id (:current-file-id @st/state)]
|
||||
(if (= lib-id current-file-id)
|
||||
(dm/get-in @st/state [:workspace-data attr])
|
||||
(dm/get-in @st/state [:workspace-libraries lib-id :data attr]))))
|
||||
(asStroke [_]
|
||||
(let [color (u/locate-library-color $file $id)]
|
||||
(u/to-js
|
||||
(d/without-nils
|
||||
{:stroke-color (:color color)
|
||||
:stroke-opacity (:opacity color)
|
||||
:stroke-color-gradient (:gradient color)
|
||||
:stroke-color-ref-file $file
|
||||
:stroke-color-ref-id $id
|
||||
:stroke-image (:image color)
|
||||
:stroke-style :solid
|
||||
:stroke-alignment :inner})))))
|
||||
|
||||
([self attr mapfn]
|
||||
(-> (get-library-data self attr)
|
||||
(mapfn))))
|
||||
(defn lib-color-proxy
|
||||
[file-id id]
|
||||
(assert (uuid? file-id))
|
||||
(assert (uuid? id))
|
||||
|
||||
(defn- array-to-js
|
||||
[value]
|
||||
(.freeze
|
||||
js/Object
|
||||
(apply array (->> value (map utils/to-js)))))
|
||||
|
||||
(deftype Library [_data]
|
||||
Object)
|
||||
|
||||
(defn create-library
|
||||
[data]
|
||||
(cr/add-properties!
|
||||
(Library. data)
|
||||
{:name "_data"
|
||||
:enumerable false}
|
||||
|
||||
{:name "id"
|
||||
:get (fn [self]
|
||||
(str (:id (obj/get self "_data"))))}
|
||||
(LibraryColorProxy. file-id id)
|
||||
{:name "$file" :enumerable false}
|
||||
{:name "$id" :enumerable false}
|
||||
{:name "id" :get (fn [_] (dm/str id))}
|
||||
|
||||
{:name "name"
|
||||
:get (fn [self]
|
||||
(get-library-info self :name))}
|
||||
:get #(-> % u/proxy->library-color :name)}
|
||||
|
||||
{:name "color"
|
||||
:get #(-> % u/proxy->library-color :color)}
|
||||
|
||||
{:name "opacity"
|
||||
:get #(-> % u/proxy->library-color :opacity)}
|
||||
|
||||
{:name "gradient"
|
||||
:get #(-> % u/proxy->library-color :gradient u/to-js)}
|
||||
|
||||
{:name "image"
|
||||
:get #(-> % u/proxy->library-color :image u/to-js)}))
|
||||
|
||||
(deftype LibraryTypographyProxy [$file $id]
|
||||
Object)
|
||||
|
||||
(defn lib-typography-proxy
|
||||
[file-id id]
|
||||
(assert (uuid? file-id))
|
||||
(assert (uuid? id))
|
||||
|
||||
(cr/add-properties!
|
||||
(LibraryTypographyProxy. file-id id)
|
||||
{:name "$file" :enumerable false}
|
||||
{:name "$id" :enumerable false}
|
||||
{:name "id" :get (fn [_] (dm/str id))}
|
||||
{:name "name"
|
||||
:get #(-> % u/proxy->library-typography :name)}))
|
||||
|
||||
(deftype LibraryComponentProxy [$file $id]
|
||||
Object)
|
||||
|
||||
(defn lib-component-proxy
|
||||
[file-id id]
|
||||
(assert (uuid? file-id))
|
||||
(assert (uuid? id))
|
||||
|
||||
(cr/add-properties!
|
||||
(LibraryComponentProxy. file-id id)
|
||||
{:name "$file" :enumerable false}
|
||||
{:name "$id" :enumerable false}
|
||||
{:name "id" :get (fn [_] (dm/str id))}
|
||||
{:name "name"
|
||||
:get #(-> % u/proxy->library-component :name)}))
|
||||
|
||||
(deftype Library [$id]
|
||||
Object)
|
||||
|
||||
(defn library-proxy
|
||||
[file-id]
|
||||
(assert (uuid? file-id) "File id not valid")
|
||||
|
||||
(cr/add-properties!
|
||||
(Library. file-id)
|
||||
{:name "$file" :enumerable false}
|
||||
|
||||
{:name "id"
|
||||
:get #(-> % u/proxy->file :id str)}
|
||||
|
||||
{:name "name"
|
||||
:get #(-> % u/proxy->file :name)}
|
||||
|
||||
{:name "colors"
|
||||
:get (fn [self]
|
||||
(array-to-js (get-library-data self :colors vals)))}
|
||||
:get
|
||||
(fn [_]
|
||||
(let [file (u/locate-file file-id)
|
||||
colors (->> file :data :colors keys (map #(lib-color-proxy file-id %)))]
|
||||
(apply array colors)))}
|
||||
|
||||
{:name "typographies"
|
||||
:get (fn [self]
|
||||
(array-to-js (get-library-data self :typographies vals)))}
|
||||
:get
|
||||
(fn [_]
|
||||
(let [file (u/locate-file file-id)
|
||||
typographies (->> file :data :typographies keys (map #(lib-typography-proxy file-id %)))]
|
||||
(apply array typographies)))}
|
||||
|
||||
{:name "components"
|
||||
:get (fn [self]
|
||||
(array-to-js (get-library-data self :components vals)))}))
|
||||
:get
|
||||
(fn [_]
|
||||
(let [file (u/locate-file file-id)
|
||||
components (->> file :data :componentes keys (map #(lib-component-proxy file-id %)))]
|
||||
(apply array components)))}))
|
||||
|
||||
(deftype PenpotLibrarySubcontext []
|
||||
Object
|
||||
@@ -80,17 +143,15 @@
|
||||
|
||||
(find [_]))
|
||||
|
||||
(defn create-library-subcontext
|
||||
(defn library-subcontext
|
||||
[]
|
||||
(cr/add-properties!
|
||||
(PenpotLibrarySubcontext.)
|
||||
{:name "local" :get
|
||||
(fn [_]
|
||||
(let [file (get @st/state :workspace-file)
|
||||
data (get @st/state :workspace-data)]
|
||||
(create-library (assoc file :data data))))}
|
||||
(library-proxy (:current-file-id @st/state)))}
|
||||
|
||||
{:name "connected" :get
|
||||
(fn [_]
|
||||
(let [libraries (get @st/state :workspace-libraries)]
|
||||
(apply array (->> libraries vals (map create-library)))))}))
|
||||
(apply array (->> libraries vals (map library-proxy)))))}))
|
||||
|
||||
Reference in New Issue
Block a user