mirror of
https://github.com/penpot/penpot.git
synced 2025-06-06 02:11:38 +02:00
✨ Changes to plugin events
This commit is contained in:
parent
60f637e947
commit
59005e3bb8
2 changed files with 52 additions and 15 deletions
|
@ -61,8 +61,8 @@
|
||||||
(deftype PenpotContext [$plugin]
|
(deftype PenpotContext [$plugin]
|
||||||
Object
|
Object
|
||||||
(addListener
|
(addListener
|
||||||
[_ type callback]
|
[_ type callback props]
|
||||||
(events/add-listener type $plugin callback))
|
(events/add-listener type $plugin callback props))
|
||||||
|
|
||||||
(removeListener
|
(removeListener
|
||||||
[_ listener-id]
|
[_ listener-id]
|
||||||
|
|
|
@ -6,15 +6,20 @@
|
||||||
|
|
||||||
(ns app.plugins.events
|
(ns app.plugins.events
|
||||||
(:require
|
(:require
|
||||||
|
[app.common.data.macros :as dm]
|
||||||
|
[app.main.data.workspace.state-helpers :as wsh]
|
||||||
[app.main.store :as st]
|
[app.main.store :as st]
|
||||||
[app.plugins.file :as file]
|
[app.plugins.file :as file]
|
||||||
[app.plugins.page :as page]
|
[app.plugins.page :as page]
|
||||||
|
[app.plugins.parser :as parser]
|
||||||
|
[app.plugins.shape :as shape]
|
||||||
|
[app.util.object :as obj]
|
||||||
[goog.functions :as gf]))
|
[goog.functions :as gf]))
|
||||||
|
|
||||||
(defmulti handle-state-change (fn [type _] type))
|
(defmulti handle-state-change (fn [type _] type))
|
||||||
|
|
||||||
(defmethod handle-state-change "finish"
|
(defmethod handle-state-change "finish"
|
||||||
[_ _ old-val new-val]
|
[_ _ old-val new-val _]
|
||||||
(let [old-file-id (:current-file-id old-val)
|
(let [old-file-id (:current-file-id old-val)
|
||||||
new-file-id (:current-file-id new-val)]
|
new-file-id (:current-file-id new-val)]
|
||||||
(if (and (some? old-file-id) (nil? new-file-id))
|
(if (and (some? old-file-id) (nil? new-file-id))
|
||||||
|
@ -22,7 +27,7 @@
|
||||||
::not-changed)))
|
::not-changed)))
|
||||||
|
|
||||||
(defmethod handle-state-change "filechange"
|
(defmethod handle-state-change "filechange"
|
||||||
[_ plugin-id old-val new-val]
|
[_ plugin-id old-val new-val _]
|
||||||
(let [old-file-id (:current-file-id old-val)
|
(let [old-file-id (:current-file-id old-val)
|
||||||
new-file-id (:current-file-id new-val)]
|
new-file-id (:current-file-id new-val)]
|
||||||
(if (identical? old-file-id new-file-id)
|
(if (identical? old-file-id new-file-id)
|
||||||
|
@ -30,7 +35,7 @@
|
||||||
(file/file-proxy plugin-id new-file-id))))
|
(file/file-proxy plugin-id new-file-id))))
|
||||||
|
|
||||||
(defmethod handle-state-change "pagechange"
|
(defmethod handle-state-change "pagechange"
|
||||||
[_ plugin-id old-val new-val]
|
[_ plugin-id old-val new-val _]
|
||||||
(let [old-page-id (:current-page-id old-val)
|
(let [old-page-id (:current-page-id old-val)
|
||||||
new-page-id (:current-page-id new-val)]
|
new-page-id (:current-page-id new-val)]
|
||||||
(if (identical? old-page-id new-page-id)
|
(if (identical? old-page-id new-page-id)
|
||||||
|
@ -38,7 +43,7 @@
|
||||||
(page/page-proxy plugin-id (:current-file-id new-val) new-page-id))))
|
(page/page-proxy plugin-id (:current-file-id new-val) new-page-id))))
|
||||||
|
|
||||||
(defmethod handle-state-change "selectionchange"
|
(defmethod handle-state-change "selectionchange"
|
||||||
[_ _ old-val new-val]
|
[_ _ old-val new-val _]
|
||||||
(let [old-selection (get-in old-val [:workspace-local :selected])
|
(let [old-selection (get-in old-val [:workspace-local :selected])
|
||||||
new-selection (get-in new-val [:workspace-local :selected])]
|
new-selection (get-in new-val [:workspace-local :selected])]
|
||||||
(if (identical? old-selection new-selection)
|
(if (identical? old-selection new-selection)
|
||||||
|
@ -46,7 +51,7 @@
|
||||||
(apply array (map str new-selection)))))
|
(apply array (map str new-selection)))))
|
||||||
|
|
||||||
(defmethod handle-state-change "themechange"
|
(defmethod handle-state-change "themechange"
|
||||||
[_ _ old-val new-val]
|
[_ _ old-val new-val _]
|
||||||
(let [old-theme (get-in old-val [:profile :theme])
|
(let [old-theme (get-in old-val [:profile :theme])
|
||||||
new-theme (get-in new-val [:profile :theme])]
|
new-theme (get-in new-val [:profile :theme])]
|
||||||
(if (identical? old-theme new-theme)
|
(if (identical? old-theme new-theme)
|
||||||
|
@ -55,23 +60,55 @@
|
||||||
"dark"
|
"dark"
|
||||||
new-theme))))
|
new-theme))))
|
||||||
|
|
||||||
|
(defmethod handle-state-change "shapechange"
|
||||||
|
[_ plugin-id old-val new-val props]
|
||||||
|
(let [shape-id (-> (obj/get props "shapeId") parser/parse-id)
|
||||||
|
old-shape (wsh/lookup-shape old-val shape-id)
|
||||||
|
new-shape (wsh/lookup-shape new-val shape-id)
|
||||||
|
|
||||||
|
file-id (:current-file-id new-val)
|
||||||
|
page-id (:current-page-id new-val)]
|
||||||
|
(if (and (identical? old-shape new-shape) (some? plugin-id) (some? file-id) (some? page-id) (some? shape-id))
|
||||||
|
::not-changed
|
||||||
|
(shape/shape-proxy plugin-id file-id page-id shape-id))))
|
||||||
|
|
||||||
|
(defmethod handle-state-change "contentsave"
|
||||||
|
[_ _ old-val new-val _]
|
||||||
|
(let [old-status (dm/get-in old-val [:persistence :status])
|
||||||
|
new-status (dm/get-in new-val [:persistence :status])]
|
||||||
|
(if (and (= :saved new-status) (not= new-status old-status))
|
||||||
|
::void ;; Changed but void
|
||||||
|
::not-changed)))
|
||||||
|
|
||||||
(defmethod handle-state-change :default
|
(defmethod handle-state-change :default
|
||||||
[_ _ _ _]
|
[_ _ _ _]
|
||||||
::not-changed)
|
::not-changed)
|
||||||
|
|
||||||
(defn add-listener
|
(defn add-listener
|
||||||
[type plugin-id callback]
|
[type plugin-id callback props]
|
||||||
(let [key (js/Symbol)
|
(let [plugin-id (parser/parse-id plugin-id)
|
||||||
callback (gf/debounce callback 10)]
|
key (js/Symbol)
|
||||||
|
|
||||||
|
;; We wrap the callback in an exception handler so the plugins
|
||||||
|
;; don't crash the application
|
||||||
|
safe-callback
|
||||||
|
(fn [value]
|
||||||
|
(try
|
||||||
|
(if (= ::void value)
|
||||||
|
(callback)
|
||||||
|
(callback value))
|
||||||
|
(catch :default cause
|
||||||
|
(.error js/console cause))))
|
||||||
|
|
||||||
|
;; We also debounce the callbacks so we don't get too many at the same time
|
||||||
|
debounced-callback (gf/debounce safe-callback 10)]
|
||||||
|
|
||||||
(add-watch
|
(add-watch
|
||||||
st/state key
|
st/state key
|
||||||
(fn [_ _ old-val new-val]
|
(fn [_ _ old-val new-val]
|
||||||
(let [result (handle-state-change type plugin-id old-val new-val)]
|
(let [result (handle-state-change type plugin-id old-val new-val props)]
|
||||||
(when (not= ::not-changed result)
|
(when (not= ::not-changed result)
|
||||||
(try
|
(debounced-callback result)))))
|
||||||
(callback result)
|
|
||||||
(catch :default cause
|
|
||||||
(.error js/console cause)))))))
|
|
||||||
|
|
||||||
;; return the generated key
|
;; return the generated key
|
||||||
key))
|
key))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue