From 896fe7e09a34cc1896e856486e804a32a581b992 Mon Sep 17 00:00:00 2001 From: Weihua Lu Date: Fri, 9 Jul 2021 16:45:51 +0800 Subject: [PATCH] ui: improve shortcut display for document mode --- src/main/frontend/components/settings.cljs | 6 +++--- src/main/frontend/components/sidebar.cljs | 10 ++++++++-- src/main/frontend/modules/shortcut/core.cljs | 1 - src/main/frontend/modules/shortcut/data_helper.cljs | 11 +++++++++++ src/main/frontend/ui.cljs | 3 +-- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/frontend/components/settings.cljs b/src/main/frontend/components/settings.cljs index 6ef5a5483..c9cefff2d 100644 --- a/src/main/frontend/components/settings.cljs +++ b/src/main/frontend/components/settings.cljs @@ -13,6 +13,7 @@ [frontend.handler.ui :as ui-handler] [frontend.handler.user :as user-handler] [frontend.modules.instrumentation.core :as instrument] + [frontend.modules.shortcut.data-helper :as shortcut-helper] [frontend.state :as state] [frontend.ui :as ui] [frontend.util :refer [classnames] :as util] @@ -170,8 +171,7 @@ config-handler/toggle-ui-show-brackets! true)]] [:div {:style {:text-align "right"}} - ;; TODO: Fetch this shortcut from config.cljs so there's one source of truth - (ui/keyboard-shortcut [:meta :c :meta :b])]]) + (ui/keyboard-shortcut (shortcut-helper/gen-shortcut-seq :ui/toggle-brackets))]]) (defn language-row [t preferred-language] [:div.it.sm:grid.sm:grid-cols-3.sm:gap-4.sm:items-start @@ -208,7 +208,7 @@ :class (classnames [{:active system-theme?}])} [:i.mode-system] [:strong "system"]]]]] [:div {:style {:text-align "right"}} - (ui/keyboard-shortcut [:t :t])]]) + (ui/keyboard-shortcut (shortcut-helper/gen-shortcut-seq :ui/toggle-theme))]]) (defn file-format-row [t preferred-format] [:div.it.sm:grid.sm:grid-cols-3.sm:gap-4.sm:items-start diff --git a/src/main/frontend/components/sidebar.cljs b/src/main/frontend/components/sidebar.cljs index 643755d92..7903652a5 100644 --- a/src/main/frontend/components/sidebar.cljs +++ b/src/main/frontend/components/sidebar.cljs @@ -32,6 +32,7 @@ [reitit.frontend.easy :as rfe] [goog.dom :as gdom] [frontend.handler.web.nfs :as nfs-handler] + [frontend.modules.shortcut.data-helper :as shortcut-dh] [cljs-drag-n-drop.core :as dnd])) (defn nav-item @@ -268,8 +269,13 @@ (ui/tippy {:html [:div.p-2 [:p.mb-2 [:b "Document mode"]] [:ul - [:li "Shift + Enter to create new block"] - [:li "Click `D` or type `t d` to toggle document mode"]]]} + [:li + [:div.inline-block.mr-1 (ui/keyboard-shortcut (shortcut-dh/gen-shortcut-seq :editor/new-line))] + [:p.inline-block "to create new block"]] + [:li + [:p.inline-block.mr-1 "Click `D` or type"] + [:div.inline-block.mr-1 (ui/keyboard-shortcut (shortcut-dh/gen-shortcut-seq :ui/toggle-document-mode))] + [:p.inline-block "to toggle document mode"]]]]} [:a.block.px-1.text-sm.font-medium.bg-base-2.rounded-md.mx-2 {:on-click state/toggle-document-mode!} "D"]))) diff --git a/src/main/frontend/modules/shortcut/core.cljs b/src/main/frontend/modules/shortcut/core.cljs index 0f3bb2103..75d253e8a 100644 --- a/src/main/frontend/modules/shortcut/core.cljs +++ b/src/main/frontend/modules/shortcut/core.cljs @@ -47,7 +47,6 @@ (log/debug :shortcut/register-shortcut {:id id :binding k}) (.registerShortcut handler (util/keyname id) k) (catch js/Object e - (def e e) (log/error :shortcut/register-shortcut {:id id :binding k :error e}) diff --git a/src/main/frontend/modules/shortcut/data_helper.cljs b/src/main/frontend/modules/shortcut/data_helper.cljs index e7ee9ab61..6a45d0023 100644 --- a/src/main/frontend/modules/shortcut/data_helper.cljs +++ b/src/main/frontend/modules/shortcut/data_helper.cljs @@ -92,8 +92,19 @@ (str/replace "mod" (if util/mac? "cmd" "ctrl")) (str/replace "alt" (if util/mac? "opt" "alt")) (str/replace "shift+/" "?") + (str/replace "open-square-bracket" "[") + (str/replace "close-square-bracket" "]") (str/lower-case))) +;; if multiple bindings, gen seq for first binding only for now +(defn gen-shortcut-seq [id] + (let [bindings (shortcut-binding id)] + (if (false? bindings) + [] + (-> bindings + first + (str/split #" |\+"))))) + (defn binding-for-display [k binding] (let [tmp (cond (false? binding) diff --git a/src/main/frontend/ui.cljs b/src/main/frontend/ui.cljs index 1a5fed40f..9fa3d8ae3 100644 --- a/src/main/frontend/ui.cljs +++ b/src/main/frontend/ui.cljs @@ -416,8 +416,7 @@ (if (or (= :meta key) (= "meta" key)) (util/meta-key-name) (name key))]) - sequence)] - ) + sequence)]) (defonce modal-show? (atom false)) (rum/defc modal-overlay