From 8583770dfcedde80aa0312b2e5c89d3390da37ac Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Mon, 24 May 2021 21:11:00 +0800 Subject: [PATCH] fix: Sometimes backspace removes two chars instead of one Close #1965 --- src/main/frontend/components/block.cljs | 2 +- src/main/frontend/modules/shortcut/core.cljs | 35 ++++++++++++++------ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 69d6e5f18..b1e810aea 100644 --- a/src/main/frontend/components/block.cljs +++ b/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)))] diff --git a/src/main/frontend/modules/shortcut/core.cljs b/src/main/frontend/modules/shortcut/core.cljs index 53c0b620f..d48e3669a 100644 --- a/src/main/frontend/modules/shortcut/core.cljs +++ b/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 []