Browse Source

feat: wip, categorize shortcuts

Weihua Lu 4 years ago
parent
commit
84cfe572f5

+ 52 - 47
src/main/frontend/components/shortcut.cljs

@@ -2,39 +2,44 @@
   (:require [rum.core :as rum]
             [frontend.context.i18n :as i18n]
             [frontend.util :as util]
-            [frontend.modules.shortcut.data-helper :as dh]))
+            [frontend.state :as state]
+            [frontend.modules.shortcut.data-helper :as dh]
+            [frontend.modules.shortcut.config :as config]))
+(def *shortcut-config (rum/cursor-in state/state [:config (state/get-current-repo) :shortcuts]))
 
-(rum/defc shortcut-table
-  [tag]
-  (rum/with-context [[t] i18n/*tongue-context*]
-    [:table
-     [:thead
-      [:tr
-       [:th [:b (t tag)]]
-       [:th (t :help/shortcut)]]]
-     [:tbody
-      (map (fn [[k {:keys [i18n binding]}]]
-             [:tr {:key k}
-              [:td (t i18n)]
-              [:td binding]])
-           (dh/binding-by-tag tag))]]))
+(rum/defc shortcut-table < rum/reactive
+  [name]
+  (let [_ (rum/react *shortcut-config)]
+    (rum/with-context [[t] i18n/*tongue-context*]
+      [:div
+       [:table
+        [:thead
+         [:tr
+          [:th [:b (t name)]]
+          [:th (t :help/shortcut)]]]
+        [:tbody
+         (map (fn [[k {:keys [i18n binding]}]]
+                [:tr {:key k}
+                 [:td (t i18n)]
+                 [:td binding]])
+              (dh/binding-by-category name))]]])))
 
 (rum/defc shortcut
   []
   (rum/with-context [[t] i18n/*tongue-context*]
     [:div
      [:h1.title (t :shortcut/page-title)]
-       [:table
-        [:thead
-         [:tr
-          [:th [:b (t :help/shortcuts-triggers)]]
-          [:th (t :help/shortcut)]]]
-        [:tbody
-         [:tr [:td (t :help/slash-autocomplete)] [:td "/"]]
-         [:tr [:td (t :help/block-content-autocomplete)] [:td "<"]]
-         [:tr [:td (t :help/reference-autocomplete)] [:td "[[]]"]]
-         [:tr [:td (t :help/block-reference)] [:td "(())"]]]]
-     (shortcut-table :shortcut.tag/basics)
+     [:table
+      [:thead
+       [:tr
+        [:th [:b (t :help/shortcuts-triggers)]]
+        [:th (t :help/shortcut)]]]
+      [:tbody
+       [:tr [:td (t :help/slash-autocomplete)] [:td "/"]]
+       [:tr [:td (t :help/block-content-autocomplete)] [:td "<"]]
+       [:tr [:td (t :help/reference-autocomplete)] [:td "[[]]"]]
+       [:tr [:td (t :help/block-reference)] [:td "(())"]]]]
+     (shortcut-table :shortcut.category/basics)
 
      #_
      [:table
@@ -60,27 +65,27 @@
        [:tr [:td (t :select-block-above)] [:td "Shift-Up"]]
        [:tr [:td (t :select-block-below)] [:td "Shift-Down"]]
        [:tr [:td (t :select-all-blocks)] [:td (util/->platform-shortcut "Ctrl-Shift-a")]]]]
-       [:table
-        [:thead
-         [:tr
-          [:th [:b (t :general)]]
-          [:th (t :help/shortcut)]]]
-        [:tbody
-         [:tr [:td (t :help/toggle)] [:td "?"]]
-         [:tr [:td (t :help/git-commit-message)] [:td "c"]]
-         [:tr [:td (t :help/full-text-search)] [:td (util/->platform-shortcut "Ctrl-u")]]
-         [:tr [:td (t :help/page-search)] [:td (util/->platform-shortcut "Ctrl-Shift-u")]]
-         [:tr [:td (t :help/open-link-in-sidebar)] [:td "Shift-Click"]]
-         [:tr [:td (t :help/context-menu)] [:td "Right Click"]]
-         [:tr [:td (t :help/fold-unfold)] [:td "Tab"]]
-         [:tr [:td (t :help/toggle-contents)] [:td "t c"]]
-         [:tr [:td (t :help/toggle-doc-mode)] [:td "t d"]]
-         [:tr [:td (t :help/toggle-theme)] [:td "t t"]]
-         [:tr [:td (t :help/toggle-right-sidebar)] [:td "t r"]]
-         [:tr [:td (t :help/toggle-settings)] [:td "t s"]]
-         [:tr [:td (t :help/toggle-insert-new-block)] [:td "t e"]]
-         [:tr [:td (t :help/jump-to-journals)] [:td (if util/mac? "Cmd-j" "Alt-j")]]]]
-       (shortcut-table :shortcut.tag/formatting)
+     [:table
+      [:thead
+       [:tr
+        [:th [:b (t :general)]]
+        [:th (t :help/shortcut)]]]
+      [:tbody
+       [:tr [:td (t :help/toggle)] [:td "?"]]
+       [:tr [:td (t :help/git-commit-message)] [:td "c"]]
+       [:tr [:td (t :help/full-text-search)] [:td (util/->platform-shortcut "Ctrl-u")]]
+       [:tr [:td (t :help/page-search)] [:td (util/->platform-shortcut "Ctrl-Shift-u")]]
+       [:tr [:td (t :help/open-link-in-sidebar)] [:td "Shift-Click"]]
+       [:tr [:td (t :help/context-menu)] [:td "Right Click"]]
+       [:tr [:td (t :help/fold-unfold)] [:td "Tab"]]
+       [:tr [:td (t :help/toggle-contents)] [:td "t c"]]
+       [:tr [:td (t :help/toggle-doc-mode)] [:td "t d"]]
+       [:tr [:td (t :help/toggle-theme)] [:td "t t"]]
+       [:tr [:td (t :help/toggle-right-sidebar)] [:td "t r"]]
+       [:tr [:td (t :help/toggle-settings)] [:td "t s"]]
+       [:tr [:td (t :help/toggle-insert-new-block)] [:td "t e"]]
+       [:tr [:td (t :help/jump-to-journals)] [:td (if util/mac? "Cmd-j" "Alt-j")]]]]
+     (shortcut-table :shortcut.category/formatting)
 
      ]
 

+ 7 - 7
src/main/frontend/dicts.cljs

@@ -157,7 +157,7 @@ title: How to take dummy notes?
         :help/toggle-settings "Toggle settings"
         :help/toggle-insert-new-block "Toggle Enter/Alt+Enter for inserting new block"
         :help/jump-to-journals "Jump to Journals"
-        :shortcut.tag/formatting "Formatting"
+        :shortcut.category/formatting "Formatting"
         :help/markdown-syntax "Markdown syntax"
         :help/org-mode-syntax "Org mode syntax"
         :bold "Bold"
@@ -353,7 +353,7 @@ title: How to take dummy notes?
         :user/delete-account-notice "All your published pages on logseq.com will be deleted."
 
         :shortcut/page-title "Learn & Customize Shortcuts"
-        :shortcut.tag/basics "Basics"
+        :shortcut.category/basics "Basics"
         }
 
    :de {:help/about "Über Logseq"
@@ -403,7 +403,7 @@ title: How to take dummy notes?
         :help/toggle-right-sidebar "Rechte Seitenleiste umschalten"
         :help/toggle-insert-new-block "Umschalten von Enter/Alt+Enter zum Einfügen eines neuen Blocks"
         :help/jump-to-journals "Zu Journalen springen"
-        :shortcut.tag/formatting "Formatierung"
+        :shortcut.category/formatting "Formatierung"
         :help/markdown-syntax "Markdown-Syntax"
         :help/org-mode-syntax "Org-Mode-Syntax"
         :bold "Fett"
@@ -629,7 +629,7 @@ title: How to take dummy notes?
         :help/toggle-right-sidebar "Afficher/cacher la barre latérale"
         :help/toggle-insert-new-block "Activer Entreée ou Alt+Enter pour insérer un bloc"
         :help/jump-to-journals "Aller au Journal"
-        :shortcut.tag/formatting "Formats"
+        :shortcut.category/formatting "Formats"
         :help/markdown-syntax "Syntaxe Markdown"
         :help/org-mode-syntax "Syntaxe Org mode"
         :bold "Gras"
@@ -892,7 +892,7 @@ title: How to take dummy notes?
            :help/toggle-settings "显示/关闭设置"
            :help/toggle-insert-new-block "切换 Enter/Alt+Enter 以插入新块"
            :help/jump-to-journals "跳转到日记"
-           :shortcut.tag/formatting "格式化"
+           :shortcut.category/formatting "格式化"
            :help/markdown-syntax "Markdown 语法"
            :help/org-mode-syntax "Org Mode 语法"
            :bold "粗体"
@@ -1172,7 +1172,7 @@ title: How to take dummy notes?
              :help/toggle-right-sidebar "啟用/關閉右側欄"
              :help/toggle-insert-new-block "切換 Enter/Alt+Enter 以插入新塊"
              :help/jump-to-journals "跳轉到日記"
-             :shortcut.tag/formatting "格式化"
+             :shortcut.category/formatting "格式化"
              :help/markdown-syntax "Markdown 語法"
              :help/org-mode-syntax "Org Mode 語法"
              :bold "粗體"
@@ -1425,7 +1425,7 @@ title: How to take dummy notes?
         :help/toggle-right-sidebar "Wissel regter sybalk"
         :help/toggle-insert-new-block "Wissel Enter/Alt+enter vir die byvoeging van nuwe blokke"
         :help/jump-to-journals "Spring na joernale"
-        :shortcut.tag/formatting "Formatering"
+        :shortcut.category/formatting "Formatering"
         :help/markdown-syntax "Markdown sintaksis"
         :help/org-mode-syntax "Org mode sintaksis"
         :bold "Vetdruk"

+ 21 - 7
src/main/frontend/modules/shortcut/config.cljs

@@ -53,18 +53,18 @@
     :editor/indent
     {:binding "tab"
      :i18n    :help/indent-block-tab
-     :tags    #{:shortcut.tag/basics}
      :fn      (editor-handler/keydown-tab-handler :right)}
     :editor/outindent
     {:binding "shift+tab"
      :i18n    :help/unindent-block
-     :tags    #{:shortcut.tag/basics}
      :fn      (editor-handler/keydown-tab-handler :left)}
     :editor/new-block
     {:binding "enter"
+     :i18n    :help/create-new-block
      :fn      editor-handler/keydown-new-block-handler}
     :editor/new-line
     {:binding "shift+enter"
+     :i18n    :help/new-line-in-block
      :fn      editor-handler/keydown-new-line-handler}
     :editor/zoom-in
     {:binding (if mac? "mod+." "alt+right")
@@ -90,21 +90,17 @@
     :editor/bold
     {:binding "mod+b"
      :i18n    :bold
-     :tags    #{:shortcut.tag/formatting}
      :fn      editor-handler/bold-format!}
     :editor/italics
     {:binding "mod+i"
      :i18n    :italics
-     :tags    #{:shortcut.tag/formatting}
      :fn      editor-handler/italics-format!}
     :editor/highlight
     {:binding "mod+shift+h"
      :i18n    :highlight
-     :tags    #{:shortcut.tag/formatting}
      :fn      editor-handler/highlight-format!}
     :editor/insert-link
-    {:binding "mod+shift+k"
-     :tags    #{:shortcut.tag/formatting}
+    {:binding "mod+k"
      :i18n    :html-link
      :fn      editor-handler/html-link-format!}
     :editor/select-all-blocks
@@ -191,6 +187,9 @@
    {:ui/toggle-brackets
     {:binding "mod+c mod+b"
      :fn      config-handler/toggle-ui-show-brackets!}
+    :go/search-in-page
+    {:binding "mod+shift+u"
+     :fn      #(route-handler/go-to-search! :page)}
     :go/search
     {:binding "mod+u"
      :fn      route-handler/go-to-search!}
@@ -241,3 +240,18 @@
     :git/commit
     {:binding "g c"
      :fn      (git-handler/show-commit-modal! commit/add-commit-message)}}})
+
+
+;; Categories for docs purpose
+(def category
+  {:shortcut.category/basics
+   [:editor/new-block
+    :editor/new-line
+    :editor/indent
+    :editor/outindent]
+
+   :shortcut.category/formatting
+   [:editor/bold
+    :editor/insert-link
+    :editor/italics
+    :editor/highlight]})

+ 1 - 0
src/main/frontend/modules/shortcut/core.cljs

@@ -31,6 +31,7 @@
     (.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)]

+ 13 - 10
src/main/frontend/modules/shortcut/data_helper.cljs

@@ -1,12 +1,13 @@
 (ns frontend.modules.shortcut.data-helper
-  (:require [frontend.modules.shortcut.config :refer [default-config]]
+  (:require [frontend.modules.shortcut.config :as config]
             [lambdaisland.glogi :as log]
             [frontend.util :as util]
             [clojure.string :as str]
             [frontend.state :as state]))
 
+
 (defn binding-map []
-  (->> (vals default-config)
+  (->> (vals config/default-config)
        (apply merge)
        (map (fn [[k {:keys [binding]}]]
               {k (or (state/get-shortcut k) binding)}))
@@ -33,14 +34,16 @@
          shortcut)
        (mapv mod-key)))))
 
-(defn binding-by-tag
-  [tag]
-  (let [keys (->> (vals default-config)
+;; returns a vector to preserve order
+(defn binding-by-category [name]
+  (let [dict (->> (vals config/default-config)
                   (apply merge)
-                  (map (fn [[k {:keys [tags] :as v}]]
-                         (when (and tags (tags tag)) k)))
-                  (remove nil?))]
-    (select-keys (binding-map) keys)))
+                  (map (fn [[k {:keys [i18n]}]]
+                         {k {:binding (get (binding-map) k)
+                             :i18n    i18n}}))
+                  (into {}))]
+    (->> (config/category name)
+         (mapv (fn [k] [k (k dict)])))))
 
 #_
 (defn decorate-namespace [k]
@@ -52,7 +55,7 @@
   ([handler-id]
    (shortcut-map handler-id nil))
   ([handler-id state]
-   (let [raw       (get default-config handler-id)
+   (let [raw       (get config/default-config handler-id)
          handler-m (->> raw
                         (map (fn [[k {:keys [fn]}]]
                                {k fn}))

+ 2 - 9
src/main/frontend/state.cljs

@@ -45,9 +45,6 @@
     :search/mode :global
     :search/result nil
 
-    ;; custom shortcuts
-    :shortcuts {:editor/new-block "enter"}
-
     ;; modals
     :modal/show? false
 
@@ -865,9 +862,7 @@
   ([key]
    (get-shortcut (get-current-repo) key))
   ([repo key]
-   (or
-    (get (storage/get (str repo "-shortcuts")) key)
-    (get-in @state [:config repo :shortcuts key]))))
+   (get-in @state [:config repo :shortcuts key])))
 
 (defn get-me
   []
@@ -1001,9 +996,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?
   ([]