Browse Source

fix: Sometimes backspace removes two chars instead of one

Close #1965
Tienson Qin 4 years ago
parent
commit
8583770dfc
2 changed files with 26 additions and 11 deletions
  1. 1 1
      src/main/frontend/components/block.cljs
  2. 25 10
      src/main/frontend/modules/shortcut/core.cljs

+ 1 - 1
src/main/frontend/components/block.cljs

@@ -394,7 +394,7 @@
                                  :text-align "left"
                                  :font-weight 500
                                  :max-height 600
-                                 :padding-bottom 200}}
+                                 :padding-bottom 64}}
                         [:h2.font-bold.text-lg page-name]
                         (let [page (db/entity [:block/name (string/lower-case page-name)])]
                           ((state/get-page-blocks-cp) (state/get-current-repo) page (:sidebar? config)))]

+ 25 - 10
src/main/frontend/modules/shortcut/core.cljs

@@ -81,27 +81,42 @@
     (swap! *installed dissoc install-id)))
 
 
+;; Sometimes backspace removes two chars instead of one
+;; https://github.com/logseq/logseq/issues/1965
+;; A workaround to ensure each shortcut group is only registered once
+
+(defonce *registered-shortcuts-state (atom nil))
+
+(defn- uninstall-shortcut-aux!
+  [state handler-id]
+  (some-> (get state :shortcut-key)
+          uninstall-shortcut!)
+  (swap! *registered-shortcuts-state dissoc handler-id))
+
+(defn- install-shortcut-aux!
+  [state handler-id]
+  (if (get @*registered-shortcuts-state handler-id)
+    state
+    (let [install-id (-> handler-id
+                        (install-shortcut! {:state state}))]
+     (swap! *registered-shortcuts-state assoc handler-id install-id)
+     (assoc state :shortcut-key install-id))))
+
 (defn mixin [handler-id]
   {:did-mount
    (fn [state]
-     (let [install-id (-> handler-id
-                          (install-shortcut! {:state state}))]
-       (assoc state :shortcut-key install-id)))
+     (install-shortcut-aux! state handler-id))
 
    :did-remount (fn [old-state new-state]
 
                   ;; uninstall
-                  (-> (get old-state :shortcut-key)
-                      uninstall-shortcut!)
+                  (uninstall-shortcut-aux! old-state handler-id)
 
                   ;; update new states
-                  (let [install-id (-> handler-id
-                                       (install-shortcut! {:state new-state}))]
-                    (assoc new-state :shortcut-key install-id)))
+                  (install-shortcut-aux! new-state handler-id))
    :will-unmount
    (fn [state]
-     (-> (get state :shortcut-key)
-         uninstall-shortcut!)
+     (uninstall-shortcut-aux! state handler-id)
      (dissoc state :shortcut-key))})
 
 (defn unlisten-all []