Browse Source

enhance: shortcut install process

Weihua Lu 4 years ago
parent
commit
362e65671a

+ 7 - 2
src/main/frontend/components/repo.cljs

@@ -11,6 +11,7 @@
             [frontend.handler.export :as export-handler]
             [frontend.handler.web.nfs :as nfs-handler]
             [frontend.handler.page :as page-handler]
+            [frontend.modules.shortcut.core :as shortcut]
             [frontend.util :as util]
             [frontend.config :as config]
             [reitit.frontend.easy :as rfe]
@@ -84,6 +85,10 @@
                  "Unlink"]]]))]]
         (widgets/add-graph)))))
 
+(defn refresh-cb []
+  (repo-handler/create-today-journal!)
+  (shortcut/refresh!))
+
 (rum/defc sync-status < rum/reactive
   {:did-mount (fn [state]
                 (js/setTimeout common-handler/check-changed-files-status 1000)
@@ -96,8 +101,7 @@
           (let [syncing? (state/sub :graph/syncing?)]
             [:div.ml-3.mr-1.mt-1.opacity-30.refresh.hover:opacity-100 {:class (if syncing? "loader" "initial")}
              [:a
-              {:on-click #(nfs-handler/refresh! repo
-                                                repo-handler/create-today-journal!)
+              {:on-click #(nfs-handler/refresh! repo refresh-cb)
                :title (str "Import files from the local directory: " (config/get-local-dir repo) ".\nVersion: "
                            version/version)}
               svg/refresh]])
@@ -217,6 +221,7 @@
                                      (state/set-current-repo! url)
                                      ;; load config
                                      (common-handler/reset-config! url nil)
+                                     (shortcut/refresh!)
                                      (when-not (= :draw (state/get-current-route))
                                        (route-handler/redirect-to-home!))
                                      (when on-click

+ 4 - 1
src/main/frontend/handler.cljs

@@ -84,6 +84,10 @@
                                    (ui-handler/add-style-if-exists!))))
                          (p/then
                           (fn []
+
+                            ;; install after config is restored
+                            (shortcut/refresh!)
+
                             (cond
                               (and (not logged?)
                                    (not (seq (db/get-files config/local-repo)))
@@ -190,7 +194,6 @@
     (reset! db/*sync-search-indice-f search/sync-search-indice!)
     (db/run-batch-txs!)
     (file-handler/run-writes-chan!)
-    (shortcut/install-shortcuts!)
     (when (util/electron?)
       (el/listen!))))
 

+ 3 - 1
src/main/frontend/handler/page.cljs

@@ -13,6 +13,7 @@
             [frontend.handler.web.nfs :as web-nfs]
             [frontend.handler.notification :as notification]
             [frontend.handler.config :as config-handler]
+            [frontend.modules.shortcut.core :as shortcut]
             [frontend.handler.ui :as ui-handler]
             [frontend.modules.outliner.file :as outliner-file]
             [frontend.modules.outliner.core :as outliner-core]
@@ -379,7 +380,8 @@
   []
   (web-nfs/ls-dir-files-with-handler!
    (fn []
-     (init-commands!))))
+     (init-commands!)
+     (shortcut/refresh!))))
 
 ;; TODO: add use :file/last-modified-at
 (defn get-pages-with-modified-at

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

@@ -35,11 +35,11 @@
     (.setAlwaysPreventDefault handler prevent-default?)
 
     ;; register shortcuts
-    ;; TODO add try catch for register conflicts
     (doseq [[id _] shortcut-map]
       ;; (log/info :shortcut/install-shortcut {:id id :shortcut (dh/shortcut-binding id)})
       (doseq [k (dh/shortcut-binding id)]
         (try
+          (log/debug :shortcut/register-shortcut {:id id :binding k})
           (.registerShortcut handler (util/keyname id) k)
           (catch js/Object e
             (log/error :shortcut/register-shortcut {:id id
@@ -64,19 +64,19 @@
 
       install-id)))
 
-(defn install-shortcuts!
+(defn- install-shortcuts!
   []
-  (install-shortcut! :shortcut.handler/misc {:skip-installed? true})
-  (->> [:shortcut.handler/editor-global
+  (->> [:shortcut.handler/misc
+        :shortcut.handler/editor-global
         :shortcut.handler/global-non-editing-only
         :shortcut.handler/global-prevent-default]
        (map #(install-shortcut! % {}))
        doall))
 
-(defn uninstall-shortcut! [install-id]
-  (let [handler
-        (-> (get @*installed install-id)
-            :handler)]
+(defn- uninstall-shortcut! [install-id]
+  (when-let
+      [handler (-> (get @*installed install-id)
+                   :handler)]
     (.dispose ^js handler)
     (swap! *installed dissoc install-id)))
 
@@ -109,11 +109,13 @@
      (dissoc state :shortcut-key))})
 
 (defn unlisten-all []
-  (doseq [{:keys [handler]} (vals @*installed)]
+  (doseq [{:keys [handler group]} (vals @*installed)
+          :when (not= group :shortcut.handler/misc)]
     (.removeAllListeners handler)))
 
 (defn listen-all []
-  (doseq [{:keys [handler dispatch-fn]} (vals @*installed)]
+  (doseq [{:keys [handler group dispatch-fn]} (vals @*installed)
+          :when (not= group :shortcut.handler/misc)]
     (events/listen handler EventType/SHORTCUT_TRIGGERED dispatch-fn)))
 
 (defn disable-all-shortcuts []
@@ -126,3 +128,11 @@
    (fn [state]
      (listen-all)
      state)})
+
+(defn refresh!
+  "Always use this function to refresh shortcuts"
+  []
+  (log/info :shortcut/refresh @*installed)
+  (doseq [id (keys @*installed)]
+    (uninstall-shortcut! id))
+  (install-shortcuts!))

+ 7 - 7
src/main/frontend/modules/shortcut/data_helper.cljs

@@ -4,11 +4,11 @@
             [frontend.state :as state]
             [frontend.util :as util]
             [lambdaisland.glogi :as log]))
-(defonce binding-map
+(defonce default-binding
   (->> (vals config/default-config)
-       (apply merge)
+       (into {})
        (map (fn [[k {:keys [binding]}]]
-              {k (or (state/get-shortcut k) binding)}))
+              {k binding}))
        (into {})))
 
 (defn- mod-key [shortcut]
@@ -17,7 +17,8 @@
 
 (defn shortcut-binding
   [id]
-  (let [shortcut (get binding-map id)]
+  (let [shortcut (get (state/shortcuts) id
+                      (get default-binding id))]
     (cond
       (nil? shortcut)
       (log/error :shortcut/binding-not-found {:id id})
@@ -36,9 +37,8 @@
 (defn binding-by-category [name]
   (let [dict (->> (vals config/default-config)
                   (apply merge)
-                  (map (fn [[k {:keys [i18n]}]]
-                         {k {:binding (get binding-map k)
-                             :i18n    i18n}}))
+                  (map (fn [[k _]]
+                         {k {:binding (shortcut-binding k)}}))
                   (into {}))]
     (->> (config/category name)
          (mapv (fn [k] [k (k dict)])))))

+ 3 - 10
src/main/frontend/state.cljs

@@ -906,13 +906,8 @@
   [repo-url value]
   (swap! state assoc-in [:git/status repo-url] value))
 
-(defn get-shortcut
-  ([key]
-   (get-shortcut (get-current-repo) key))
-  ([repo key]
-   (or
-    (get (storage/get (str repo "-shortcuts")) key)
-    (get-in @state [:config repo :shortcuts key]))))
+(defn shortcuts []
+  (get-in @state [:config (get-current-repo) :shortcuts]))
 
 (defn get-me
   []
@@ -1056,9 +1051,7 @@
 
 (defn set-config!
   [repo-url value]
-  (set-state! [:config repo-url] value)
-  (let [shortcuts (or (:shortcuts value) {})]
-    (storage/set (str repo-url "-shortcuts") shortcuts)))
+  (set-state! [:config repo-url] value))
 
 (defn get-git-auto-push?
   ([]