Răsfoiți Sursa

fix: shortcuts

Tienson Qin 4 ani în urmă
părinte
comite
09128b19b6

+ 2 - 1
src/main/frontend/handler/route.cljs

@@ -94,7 +94,8 @@
 (defn update-page-label!
   [route]
   (let [{:keys [data]} route]
-    (set! (. js/document.body.dataset -page) (name (:name data)))))
+    (when-let [data-name (:name data)]
+      (set! (. js/document.body.dataset -page) (name data-name)))))
 
 (defn jump-to-anchor!
   [anchor-text]

+ 19 - 15
src/main/frontend/modules/shortcut/core.cljs

@@ -23,6 +23,12 @@
 
 (def key-names (js->clj KeyNames))
 
+(defn- get-handler-by-id
+  [handler-id]
+  (-> (some #(= (:group %) handler-id) (vals @*installed))
+      first
+      :handler))
+
 (defn register-shortcut!
   "Register a shortcut, notice the id need to be a namespaced keyword to avoid
   conflicts.
@@ -35,8 +41,7 @@
   ([handler-id id shortcut-map]
    (when-let [handler (if (or (string? handler-id) (keyword? handler-id))
                         (let [handler-id (keyword handler-id)]
-                          (-> (get @*installed handler-id)
-                              :handler))
+                          (get-handler-by-id handler-id))
                         ;; handler
                         handler-id)]
 
@@ -59,19 +64,18 @@
   Example:
   (unregister-shortcut! :shortcut.handler/misc :foo/bar)"
   [handler-id shortcut-id]
-  (when-let [handler (-> (get @*installed handler-id)
-                         :handler)]
+  (when-let [handler (get-handler-by-id handler-id)]
     (when shortcut-id
       (let [k (dh/shortcut-binding shortcut-id)]
         (.unregisterShortcut ^js handler k))
       (shortcut-config/remove-shortcut! handler-id shortcut-id))))
 
 (defn uninstall-shortcut!
-  [handler-id]
-  (when-let [handler (-> (get @*installed handler-id)
+  [install-id]
+  (when-let [handler (-> (get @*installed install-id)
                          :handler)]
     (.dispose ^js handler)
-    (swap! *installed dissoc handler-id)))
+    (swap! *installed dissoc install-id)))
 
 (defn install-shortcut!
   [handler-id {:keys [set-global-keys?
@@ -81,7 +85,6 @@
                :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
@@ -100,7 +103,7 @@
                     dispatch-fn (get shortcut-map (keyword (.-identifier e)))]
                 ;; trigger fn
                 (when dispatch-fn (dispatch-fn e))))
-          install-id handler-id
+          install-id (medley/random-uuid)
           data       {install-id
                       {:group      handler-id
                        :dispatch-fn f
@@ -125,16 +128,17 @@
 (defn mixin [handler-id]
   {:did-mount
    (fn [state]
-     (js/setTimeout #(install-shortcut! handler-id {:state state}) 20)
-     state)
+     (let [install-id (install-shortcut! handler-id {:state state})]
+       (assoc state ::install-id install-id)))
 
    :did-remount (fn [old-state new-state]
-                  (uninstall-shortcut! handler-id)
-                  (js/setTimeout #(install-shortcut! handler-id {:state new-state}) 20)
-                  new-state)
+                  (uninstall-shortcut! (::install-id old-state))
+                  (when-let [install-id (install-shortcut! handler-id {:state new-state})]
+                    (assoc new-state ::install-id install-id)))
    :will-unmount
    (fn [state]
-     (uninstall-shortcut! handler-id)
+     (when-let [install-id (::install-id state)]
+       (uninstall-shortcut! install-id))
      state)})
 
 (defn unlisten-all []