From 1e68d4ec87f95dfe502cec9e53663e9839a80871 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Thu, 10 Oct 2024 13:14:21 +0200 Subject: [PATCH] :sparkles: Close plugin on esc button --- frontend/src/app/main/data/plugins.cljs | 33 ++++++++++++++++++- .../app/main/data/workspace/shortcuts.cljs | 16 ++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/frontend/src/app/main/data/plugins.cljs b/frontend/src/app/main/data/plugins.cljs index a2c2db7cb..97d286040 100644 --- a/frontend/src/app/main/data/plugins.cljs +++ b/frontend/src/app/main/data/plugins.cljs @@ -6,14 +6,32 @@ (ns app.main.data.plugins (:require + [app.common.data :as d] + [app.common.data.macros :as dm] + [app.main.store :as st] [app.plugins.register :as pr] [app.util.globals :as ug] [beicon.v2.core :as rx] [potok.v2.core :as ptk])) +(defn save-current-plugin + [id] + (ptk/reify ::save-current-plugin + ptk/UpdateEvent + (update [_ state] + (update-in state [:workspace-local :open-plugins] (fnil conj #{}) id)))) + +(defn remove-current-plugin + [id] + (ptk/reify ::remove-current-plugin + ptk/UpdateEvent + (update [_ state] + (update-in state [:workspace-local :open-plugins] (fnil disj #{}) id)))) + (defn open-plugin! [{:keys [plugin-id name description host code icon permissions]}] (try + (st/emit! (save-current-plugin plugin-id)) (.ɵloadPlugin ^js ug/global #js {:pluginId plugin-id @@ -22,8 +40,12 @@ :host host :code code :icon icon - :permissions (apply array permissions)}) + :permissions (apply array permissions)} + (fn [] + (st/emit! (remove-current-plugin plugin-id)))) + (catch :default e + (st/emit! (remove-current-plugin plugin-id)) (.error js/console "Error" e)))) (defn close-plugin! @@ -33,6 +55,15 @@ (catch :default e (.error js/console "Error" e)))) +(defn close-current-plugin + [] + (ptk/reify ::close-current-plugin + ptk/EffectEvent + (effect [_ state _] + (let [ids (dm/get-in state [:workspace-local :open-plugins])] + (doseq [id ids] + (close-plugin! (pr/get-plugin id))))))) + (defn delay-open-plugin [plugin] (ptk/reify ::delay-open-plugin diff --git a/frontend/src/app/main/data/workspace/shortcuts.cljs b/frontend/src/app/main/data/workspace/shortcuts.cljs index 574108769..fd045067a 100644 --- a/frontend/src/app/main/data/workspace/shortcuts.cljs +++ b/frontend/src/app/main/data/workspace/shortcuts.cljs @@ -6,9 +6,11 @@ (ns app.main.data.workspace.shortcuts (:require + [app.common.data.macros :as dm] [app.main.data.events :as ev] [app.main.data.exports :as de] [app.main.data.modal :as modal] + [app.main.data.plugins :as dpl] [app.main.data.preview :as dp] [app.main.data.shortcuts :as ds] [app.main.data.users :as du] @@ -28,6 +30,7 @@ [app.main.store :as st] [app.main.ui.hooks.resize :as r] [app.util.dom :as dom] + [beicon.v2.core :as rx] [potok.v2.core :as ptk])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -44,6 +47,17 @@ (when-not (deref refs/workspace-read-only?) (run! st/emit! events))) +(def esc-pressed + (ptk/reify ::esc-pressed + ptk/WatchEvent + (watch [_ state _] + (rx/of + :interrupt + (let [selection (dm/get-in state [:workspace-local :selected])] + (if (empty? selection) + (dpl/close-current-plugin) + (dw/deselect-all true))))))) + ;; Shortcuts format https://github.com/ccampbell/mousetrap (def base-shortcuts @@ -111,7 +125,7 @@ :escape {:tooltip (ds/esc) :command "escape" :subsections [:edit] - :fn #(st/emit! :interrupt (dw/deselect-all true))} + :fn #(st/emit! esc-pressed)} ;; MODIFY LAYERS