Browse Source

fix: shortcut could be triggered multiple times

Tienson Qin 3 years ago
parent
commit
0e7712ac0f
2 changed files with 12 additions and 6 deletions
  1. 0 1
      src/main/frontend/handler/repo.cljs
  2. 12 5
      src/main/frontend/modules/shortcut/core.cljs

+ 0 - 1
src/main/frontend/handler/repo.cljs

@@ -430,7 +430,6 @@
      (global-config-handler/restore-global-config!))
     ;; Don't have to unlisten the old listener, as it will be destroyed with the conn
    (db/listen-and-persist! repo)
-   (state/pub-event! [:shortcut/refresh])
    (ui-handler/add-style-if-exists!)
    (state/set-db-restoring! false)))
 

+ 12 - 5
src/main/frontend/modules/shortcut/core.cljs

@@ -98,6 +98,10 @@
                :or   {set-global-keys? true
                       prevent-default? false
                       skip-installed? false}}]
+  (when-let [install-id (ffirst (filter (fn [[id m]]
+                                          (= handler-id (:group m))) @*installed))]
+    (uninstall-shortcut! install-id))
+
   (let [shortcut-map (dh/shortcut-map handler-id state)
         handler      (new KeyboardShortcutHandler js/window)]
     ;; set arrows enter, tab to global
@@ -178,11 +182,14 @@
 (defn refresh!
   "Always use this function to refresh shortcuts"
   []
-  (log/info :shortcut/refresh @*installed)
-  (doseq [id (keys @*installed)]
-    (uninstall-shortcut! id))
-  (install-shortcuts!)
-  (state/pub-event! [:shortcut-handler-refreshed]))
+  (when-not (:ui/shortcut-handler-refreshing? @state/state)
+    (state/set-state! :ui/shortcut-handler-refreshing? true)
+    (log/info :shortcut/refresh @*installed)
+    (doseq [id (keys @*installed)]
+      (uninstall-shortcut! id))
+    (install-shortcuts!)
+    (state/pub-event! [:shortcut-handler-refreshed])
+    (state/set-state! :ui/shortcut-handler-refreshing? false)))
 
 (defn- name-with-meta [e]
   (let [ctrl    (.-ctrlKey e)