Browse Source

fix: repeated shortcut registration

Tienson Qin 4 years ago
parent
commit
7cb8c6fc9f
1 changed files with 13 additions and 28 deletions
  1. 13 28
      src/main/frontend/modules/shortcut/core.cljs

+ 13 - 28
src/main/frontend/modules/shortcut/core.cljs

@@ -66,6 +66,13 @@
         (.unregisterShortcut ^js handler k))
       (shortcut-config/remove-shortcut! handler-id shortcut-id))))
 
+(defn uninstall-shortcut!
+  [handler-id]
+  (when-let [handler (-> (get @*installed handler-id)
+                         :handler)]
+    (.dispose ^js handler)
+    (swap! *installed dissoc handler-id)))
+
 (defn install-shortcut!
   [handler-id {:keys [set-global-keys?
                       prevent-default?
@@ -74,9 +81,10 @@
                :or   {set-global-keys? true
                       prevent-default? false
                       skip-installed? false}}]
+  (uninstall-shortcut! handler-id)
   (let [shortcut-map (dh/shortcut-map handler-id state)
         handler      (new KeyboardShortcutHandler js/window)]
-     ;; set arrows enter, tab to global
+    ;; set arrows enter, tab to global
     (when set-global-keys?
       (.setGlobalKeys handler global-keys))
 
@@ -114,39 +122,16 @@
        (map #(install-shortcut! % {}))
        doall))
 
-(defn uninstall-shortcut!
-  [handler-id]
-  (when-let [handler (-> (get @*installed handler-id)
-                         :handler)]
-    (.dispose ^js handler)
-    (swap! *installed dissoc handler-id)))
-
-(defn- uninstall-shortcut-aux!
-  [state handler-id]
-  (some-> (get state :shortcut-key)
-          uninstall-shortcut!))
-
-(defn- install-shortcut-aux!
-  [state handler-id]
-  (let [install-id (-> handler-id
-                       (install-shortcut! {:state state}))]
-    (assoc state :shortcut-key install-id)))
-
 (defn mixin [handler-id]
   {:did-mount
    (fn [state]
-     (install-shortcut-aux! state handler-id))
-
-   :did-remount (fn [old-state new-state]
-                  ;; uninstall
-                  (uninstall-shortcut-aux! old-state handler-id)
+     (js/setTimeout #(install-shortcut! handler-id {:state state}) 20)
+     state)
 
-                  ;; update new states
-                  (install-shortcut-aux! new-state handler-id))
    :will-unmount
    (fn [state]
-     (uninstall-shortcut-aux! state handler-id)
-     (dissoc state :shortcut-key))})
+     (uninstall-shortcut! handler-id)
+     state)})
 
 (defn unlisten-all []
   (doseq [{:keys [handler group]} (vals @*installed)